Sunday 15 June 2014

regex - Which characters to escape when using RegExp object in JavaScript? -


I have regex ^ \ (\ d + \) which matches the number of parentheses It is designed for what it hopes and does.

I want to extend it which is used to match the digits in brackets, followed by a string that comes from a variable. I know that I can crimp a RegExp object to do this, but I'm having trouble understanding what to do to get this work done. Has anyone explained this to me?

When you RegEx , you have a literal signaling

/ ^ \ (\ d + \) /

But when you use RegEx constructor By using, you will have to double these symbols:

var re = new RegExp ("^ \\ (\\ d + \\)");

:

There are 2 ways to create a RegExp object: To indicate a literal notation and a constructor string, the parameters of the controller function are quoted in quotation marks Using the parameters of the literal notation, do not use quotation marks.

... When the regular expression remains constant,

... when you change the regular expression patterns, then you use the constructor function> or you do not know the pattern and Getting it from another source, such as user input

Now, if you have a variable, then it is good to stick to the constructor method.

  var re = new RegExp ('^ \\ (\\ d + \\)' + variable);  

It is mandatory to avoid slash because it is a self-evacuation symbol.


No comments:

Post a Comment