regex - How can I extract a number from a string in JavaScript?

Опубликовано: 02 Октябрь 2024
на канале: Code Samples
120
2

I have a string in JavaScript (e.g., #box2), and I just want the 2 from it.
I tried:
var thestring = $(this).attr('href');
var thenum = thestring.replace(/(^.+)(\w\d+\w)(.+$)/i, '$2');
alert(thenum);

It still returns #box2 in the alert. How can I get it to work?
It needs to accommodate for any length number attached on the end.