Why does this regex return an entirely different result in javascript as compared to an on-line regex tester, found at http://www.gskinner.com/RegExr/
var patt = new RegExp(/\D([0-9]*)/g);
"/144444455".match(patt);
The return in the console is:
["/144444455"]
While it does return the correct group in the regexr tester.
All I’m trying to do is extract the first amount inside a piece of text. Regardless if that text starts with a “/” or has a bunch of other useless information.
The regex does exactly what you tell it to:
\Dmatches a non-digit (in this case/)[0-9]*matches a string of digits (144444455)You will need to access the content of the first capturing group:
Or simply drop the
\Dentirely – I’m not sure why you think you need it in the first place…Then, you should probably remove the
/gmodifier if you only want to match the first number, not all numbers in your text. So,should work just as well.