So, I'm trying to write a regex that matches all numbers. Here is that regex:
And I try to use it on the string:
100 two 100
And everything works fine; it matches both of the numbers.
But I want to rewrite the regex in the form:
Because I think it looks clearer. So I write it like this:
new RegExp('\b[\d \.]+\b','g')
But now it won't match the former test string. I have tried everything, but I just can't get it to work. What am I doing wrong?
Please ignore my close vote. Your problem is that the backslash in a string has a special meaning; if you want a backslash in your regexp, you first need to get literal backslashes in the string passed to the regex:
new RegExp('\\b[\\d \\.]+\\b','g');
Note that this is a pretty bad (permissive) regex, as it will match
". . . " as a 'number', or
"1 1...3 42". Better might be:
Note that this matches odd things like
0000.3 also does not match:
/.../ notation fully clear.
©2020 All rights reserved.