Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > any way to escape the expression when constructing a RegExp object?

Reply
Thread Tools

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

 
 
darren
Guest
Posts: n/a
 
      05-26-2008
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.
 
Reply With Quote
 
 
 
 
Bjoern Hoehrmann
Guest
Posts: n/a
 
      05-26-2008
* 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.
--
Björn Höhrmann · private.php?do=newpm&u= · http://bjoern.hoehrmann.de
Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/
 
Reply With Quote
 
 
 
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      05-26-2008
darren <> writes:

> 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
--
Lasse Reichstein Nielsen -
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shortest way of constructing array of members of a class? ardi C++ 1 07-02-2012 12:37 PM
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C++ 42 11-04-2008 12:39 PM
How to read strings cantaining escape character from a file and useit as escape sequences? slomo Python 5 12-02-2007 11:39 AM
501 PIX "deny any any" "allow any any" Any Anybody? Networking Student Cisco 4 11-16-2006 10:40 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57