RegExp and special caracters
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.javascript archive

RegExp and special caracters

From: arno <arno.@no-log.org>
Date: Sat Feb 04 2006 - 12:06:06 CET

Hi,

I want to search a substring within a string :

fonction (str, substr) {
        if (str.search(substr) != -1) {
                // do something
        }
}

My problem is that my string (and my substring) may contain the special
caracter : $
ex

substr = 'a$b';
str = 'xxxa$bxxx'

So in str.search(substr), the $ will be considered as an end of line
assertion, and the search won't succeed.

I found a solution by first escaping the $ :

fonction (str, substr) {
        var substrRe = substr.replace(/\$/, '\\$$');
        if (str.search(substrRe) != -1) {
                // do something
        }
}

but I'd like to known if you think of a better way to do it, for example
is there a notation, or a flag that allows a regexp to not consider the
special caracters.

thanx
Received on Tue Feb 7 21:34:04 2006