I’m using jQuery to retrieve the class attribute and I need to get the a substring of the digits that follow the substring "position"
for example
"position7 selected" I need to retrieve "7"
for example
"navitem position14 selected" I need to retrieve "14"
I started writing:
$(this).attr('class').match(/(\d+)$/))
But I am getting lost with the regular expression, any help much appreciated.
I actually really really like regular expressions but I’m still learning!
update due to first answer: there might be another group of digits which I need to ignore
For example “navitem2 position14 selected” I need to retrieve “14”
The call returns
["position14", "14"], so the[1]element is 14.You’re on the right track with the
(\d+), it will match a contiguous group of digits. This just says only match that group when it directly follows the literal string"position".