javascript \d regular expression unexpected behavior

I am trying use javascript regular expressions to do some matching and I found a really unusual behavior that I was hoping someone could explain.

The string I was trying to match was: " 0 (IR) " and the code block was

finalRegEx = new RegExp("[0-9]");
match = finalRegEx.exec(str);

except that when I put "\d" instead of "[0-9]" it didn't find a match. I'm really confused by this.


If you use RegExp with "\d" to build the regular expression, the "\d" will result in just "d". Either use two back slashes to escape the slash like "\\d" or simply use the regular expression literals /…/ instead:

match = /\d/.exec(str)

You need to escape it because you're using the constructor, otherwise it matches d literally:

new RegExp('\\d').test('1')

new RegExp should only be used for dynamic matching. Otherwise use a literal:

var foo = /\d/;

You probably need to escape the backslash: finalRegEx = new RegExp("\\d");

Need Your Help

Xcode Button to Launch URL

xcode button

I've created a simple app that has a button in the bottom toolbar and I can't seem to get the button to work. I've tried to attach an action to it that will open a URL when pressed but it did not w...

How to change the schema on a dynamic per query basis?

c# entity-framework

I want to be able to pass the name of the schema to all of my data layer methods, and somehow have Entity Framework change the schema on a per query basis.