So im trying some really basic RegEx out for the first time and I've been told that theres a few ways of stating the start and end of the string.
One way would be ' \\A
' & ' \\Z
' and the other would be ' ^
' & ' $
'.
For some reason when running this in JS, the later is the only option that actually works.
Does anyone know why that might be please?
var str = "123456",
pattern1 = new RegExp("^\\d{6}$"),
pattern2 = new RegExp("\A\\d{6}\Z");
if(pattern1.test(str)){
alert('pattern 1 match!');
}else{
alert('pattern 1 no match!');
}
if(pattern2.test(str)){
alert('pattern 2 match!');
}else{
alert('pattern 2 no match!');
}
I think whoever told you that \\A
and \\Z
are supposed to work was misinformed. Based on the JavaScript Regular Expressions documentation , only ^
and $
should work.
Have a little play with a regex visualizer, like Debuggex . It shows that your expression matches when PCRE or Python is selected as the regex flavor (in the drop-down), but doesn't match when using the Javascript flavor.
In short, Javascript simply doesn't support \\A
and \\Z
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.