Javascript regular expression - string to RegEx object

前端 未结 1 1714
心在旅途
心在旅途 2020-12-09 09:28

I am sure its something pretty small that I am missing but I haven\'t been able to figure it out.

I have a JavaScript variable with the regex pattern in it but I can

相关标签:
1条回答
  • 2020-12-09 09:37

    Backslashes are special characters in strings that need to be escaped with another backslash:

    var value = "someone@something.com";
    var pattern = "^\\w+@[a-zA-Z_]+?\\.[a-zA-Z]{2,3}$"
    var re = new RegExp(pattern);
    re.test(value);
    
    0 讨论(0)
提交回复
热议问题