any way to escape the expression when constructing a RegExp object?

D

darren

Hi

We have some code that makes a RegExp object out of a variable:

var xxx = new RegExp(aString);

The problem is that sometimes the string contains regular expression
characters so the match isn't happening as we would like. For example
I want to match the literal string "[1-1]" but when this is the
variable that is made into a RegExp, I think it is interpreted as a
range because of the [] characters. Is there anyway I can tell the
constructor that I want to match [1-1]? I basically want to escape
any special characters: \[1-1\]

thanks for any help.
 
B

Bjoern Hoehrmann

* darren wrote in comp.lang.javascript:
We have some code that makes a RegExp object out of a variable:

var xxx = new RegExp(aString);

The problem is that sometimes the string contains regular expression
characters so the match isn't happening as we would like. For example
I want to match the literal string "[1-1]" but when this is the
variable that is made into a RegExp, I think it is interpreted as a
range because of the [] characters. Is there anyway I can tell the
constructor that I want to match [1-1]? I basically want to escape
any special characters: \[1-1\]

Note that in many cases if you don't use any regular expression features
you are better off not using regular expressions but some other native
method. There is no method that does as you ask. What you can do though
is change the string so that all the special characters are escaped.
 
L

Lasse Reichstein Nielsen

darren said:
We have some code that makes a RegExp object out of a variable:

var xxx = new RegExp(aString);

The problem is that sometimes the string contains regular expression
characters so the match isn't happening as we would like. For example
I want to match the literal string "[1-1]" but when this is the
variable that is made into a RegExp, I think it is interpreted as a
range because of the [] characters. Is there anyway I can tell the
constructor that I want to match [1-1]? I basically want to escape
any special characters: \[1-1\]

The short answer is that if you need to look for a exact match,
you don't need regexps.
Just check
if (someString.indexOf(aString) >= 0) ...

If the reason you want to create a regexp to match a literal string is
that you have some generic function that takes a regexp as argument in
order to test against strings (this happens quite often), then you should
increase the abstraction level and change the function to accept a test
function instead of a regexp.

And finally, if you can't change anything, you can escape all
meaningfull characters of the string:

function escapeRegExp(literal) {
return literal.replaceAll(/([[(){.*+?\\$^|])/g,"\\$1");
}

/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top