A
arno
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
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