Different regex behaviour in web page and Chrome extension
I am trying out this regex on this page: http://www.regular-expressions.info/javascriptexample.html
where Regex= (?:http://)?(?:www.)?facebook.com/([w-.]*)?
and Subject String = http://www.facebook.com/xxxxxx
This returns me two matches: http://www.facebook.com/xxxxxx
and xxxxxx
The same javascript I have embedded in my chrome extension, however there it shows me only one match: 'http://www.facebook.com/' . Any ideas ? Following is the code:
var re = new RegExp("(?:http://)?(?:www.)?facebook.com/(?:profile.php?id=(?=d.*))?([w-]*)?");
var m = re.exec("http://www.facebook.com/xxxxx");
if (m == null) {
alert("No match");
} else {
var s = "Match at position " + m.index + ":n";
for (i = 0; i < m.length; i++) {
s = s + m[i] + "n";
}
alert(s);
}
When regexp comes from a string, each backslash should be masked:
var re = new RegExp("(?:http://)?(?:www.)?facebook.com/(?:profile.php?id=(?=d.*))?([w-]*)?");
In javascript you can also create regexp patterns without strings:
var re = /(?:http://)?(?:www.)?facebook.com/(?:profile.php?id=(?=d.*))?([w-]*)?/;
链接地址: http://www.djcxy.com/p/74836.html
上一篇: 原子团和非原子团