0

Why isn't my code around the test function in Object RegExp working the same way as the regex pattern. Am I missing something or Am I using the wrong escape regex

<html>
<body>

<script type="text/javascript">

var str = "info@test.com";

//This isn't working
var regStr = "^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$"; //This string can be any regex get from XSLT

//Escape function get from: http://stackoverflow.com/a/6969486/193850
regStr = regStr.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
console.log(regStr); // \^\(\[w\-\]\+\(\?:\.\[w\-\]\+\)\*\)@\(\(\?:\[w\-\]\+\.\)\*w\[w\-\]\{0,66\}\)\.\(\[a\-z\]\{2,6\}\(\?:\.\[a\-z\]\{2\}\)\?\)\$
var re = new RegExp(regStr , "i");
console.log(re.test(str)); //false


var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
console.log(filter.test(str)); //true

</script>

</body>
</html>
Charles
  • 50,943
  • 13
  • 104
  • 142
Michel
  • 9,220
  • 13
  • 44
  • 59

2 Answers2

3

You have to double your backslashes when you write a regular expression as a string.

Why? The string literal syntax also observes its own backslash-quoting convention, for things like quote characters, newlines, etc. Therefore, when JavaScript parses your string constant that contains the regular expression, the backslashes will disappear. Thus, you need to quote them with another backslash so that when you pass the string to the RegExp constructor it sees the regular expression you actually intended.

Pointy
  • 405,095
  • 59
  • 585
  • 614
2

See, there's a confusion. The function you've used is a nice way of preprocessing your string-to-be regexes so you don't have to worry about escaping regex metacharacters - i.e., symbols that will control the regex behaviour, and not just taken literally.

But the point is that your string has already been parsed before it was taken by this escaping function: \w and \. sequences became just w and . respectively, the preceding slash was lost.

For characters not listed in Table 2.1, a preceding backslash is ignored, but this usage is deprecated and should be avoided.

The escaper function, actually, did restore the slash before the ., but w wasn't special for it in any kind. ) Therefore the string that went into RegExp constructor had [w] instead of [\w].

It's actually quite easy to check: just console.log(regStr) after the replacement operation.

raina77ow
  • 103,633
  • 15
  • 192
  • 229
  • So my example regex string should be: `"^([\\w-]+(?:\\.[\\w-]+)*)@((?:[\\w-]+\\.)*\\w[\\w-]{0,66})\\.([a-z]{2,6}(?:\\.[a-z]{2})?)$" ` Is for this replacement a new regex. `regStr= regStr.replace("/\\/g", "\\\\");` doesn't seems to work – Michel Jul 03 '12 at 13:57
  • It's quite enough, I suppose, just to escape the backslashes in the original string - and not use escaper function at all (or use the longer version from the same answer). – raina77ow Jul 03 '12 at 14:10
  • Thank you. I will let the user double backslash there backslashes – Michel Jul 03 '12 at 14:31