Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In need your help for a simple javascript regex

I need to get a specific class with javascript. This class is bootstrap's grid : col-md-8

I need to get the number, I try this :

$class = $('div').attr('class');
$number = $class.match(/^col-md-./);

I'm real beginner in regex, please help me.

EDIT : $class may have more class like : 'col col-md-8 col-offset-md-8'

like image 582
dmassiani Avatar asked Mar 15 '26 17:03

dmassiani


2 Answers

\d+ as pattern.

'col-md-8'.match(/\d+/)
// => ["8"]
'col-md-8'.match(/\d+/)[0]
// => "8"
  • \d match a digit character.
  • + is repetition modifier: makes previous pattern to match multiple (1 or more) times

UPDATE response to the the comment:

Using capturing group:

'other-class-1 col-md-8'.match(/col-md-(\d+)/)[1]
// => "8"
like image 59
falsetru Avatar answered Mar 18 '26 07:03

falsetru


Use simply this: $class.substr(7)

like image 34
Roman Hocke Avatar answered Mar 18 '26 07:03

Roman Hocke



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!