I'm iterating over links and chose what I need by regex.
var str = "http://([^.]*).time.com/($|(page/\d/))";
var reg = new RegExp(str); var arr = [], l = document.links;
for(var i=0; i<l.length; i++) {
console.log(l[i].href + '\t\t\t-' + reg.test(l[i].href));
}
>...
>http://newsfeed.time.com/page/3/ -false
>...
But:
/http:\/\/([^.]*).time.com\/($|(page\/\d\/))/.test('http://newsfeed.time.com/page/3/')
>true
What am I doing wrong? :) Thank you.
.characters in the regex. Again, it's two backslashes in string literals, one in regex literals. (Except that the first.doesn't need escaping because it's in a character class so the rules are different.) NB: Little things like this can sometimes compromise security. For example, if your code assumes that any URL matching this regex is trustworthy, a malicious person could circumvent it by registering the URLhttp://my-malicious-url-time.com/, which matches the regex as you've written it.