What is the difference between using “new RegExp” and using forward slash notation to create a regular expression?

六眼飞鱼酱① 提交于 2019-11-26 21:13:23

问题


Is there any difference between using new RegExp("regex"); and /same_regex/ to test against a target string? I am asking this question because I got different validating result while use these two approaches. Here is the snippet I used to validate an email field:


var email="didxga@gmail.comblah@foo.com";

var regex1 = new RegExp("^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$"); 

var regex2 = /^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/;

//using RegExp object
if(regex1.test(email))  {
       console.log("email matched regex1");    
  } else {
       console.log("email mismatched regex1");   
  }
//using slash notation
if(regex2.test(email))  {
       console.log("email matched regex2");   
  } else {  
       console.log("email mismatched regex2");
  }

I got two inconsistent results:


email matched regex1
email mismatched regex2

I am wondering if there is any difference here or I omitted something in this specific example?
For an executable example please refer to here


回答1:


If you use the constructor to create a new RegExp object instead of the literal syntax, you need to escape the \‍ properly:

new RegExp("^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$")

This is necessary as in JavaScript any unknown escape sequence \x is interpreted as x. So in this case the \. is interpreted as ..




回答2:


/.../ is called a regular expression literal. new RegExp uses the RegExp constructor function and creates a Regular Expression Object.

From Mozilla's developer pages

Regular expression literals provide compilation of the regular expression when the script is evaluated. When the regular expression will remain constant, use this for better performance.

Using the constructor function provides runtime compilation of the regular expression. Use the constructor function when you know the regular expression pattern will be changing, or you don't know the pattern and are getting it from another source, such as user input.




回答3:


this will be a help for you http://www.regular-expressions.info/javascript.html see the 'How to Use The JavaScript RegExp Object' section

if you are using RegExp(regx) regx should be in string format ex:- \w+ can be created as regx = /\w+/ or as regx = new RegExp("\\w+").




回答4:


Difference is in escaping at least in your case. When you use / / notation, you have to escape '/' with '\/', when you're using Regexp notation you escape quotes



来源:https://stackoverflow.com/questions/6149770/what-is-the-difference-between-using-new-regexp-and-using-forward-slash-notati

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!