Javascript Regex - \\d works but not \d -
This is why I am trying to exclude some real regx for the first time and I am told that ' \ D
'indicates matching of one digit.
Please someone can explain why only one pattern below works with a double back slash " \\ d
", t.
After viewing in the JavaScript Regular Expressions documentation, it includes a wicked back slash while following you.
var str = "123456", pattern1 = new RegExp ("^ \\ d {6} $"), pattern 2 = new RegExp ("^ \ d {6} $") ; If (pattern1.test (str)) {Warning ('Pattern 1 match!'); } And {warnings ('Pattern 1 no match!'); } If (pattern2.test (str)) {Warning ('Pattern Match 2!'); } And {warnings ('Pattern 2 is no match!'); }
You do not need to wrap a regular expression in quotes if you go ahead Grow and end /
Pattern 2 = New RegExp (/ ^ \ d {6} $ /);
Additional \
is required because it is in other \
Comments
Post a Comment