I'm using Node v.0.8.8.
In the following example, why url isn't being matched with /([\w-]+)
regular expression? Is it a bug or am I doing something incorrect?
var patterns = [
'/([\w-]+)',
'/.*'
],
url = '/asdf';
for (var pattern in patterns) {
var re, match;
re = new RegExp('^' + patterns[pattern] + '$');
if ((match = re.exec(url))) {
console.log(url + ' matched: ' + patterns[pattern])
// break the loop since we've already found the first match
return false;
}
else{
console.log(url + ' NOT matched: ' + patterns[pattern])
}
}
Since you create the regex from string literal, you need to escape the backslash:
'/([\\w-]+)'