Re: RegExp and special caracters
- From: Thomas 'PointedEars' Lahn <PointedEars@xxxxxx>
- Date: Sat, 04 Feb 2006 13:25:57 +0100
arno wrote:
I want to search a substring within a string :^
fonction (str, substr) {
Syntax error, should be more like
function identifier(str, substr)
{
if (str.search(substr) != -1) {
// do something
}
}
Do not use the Tab character for indentation, at least in postings. You
see above what can happen if you do.
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,
Actually, it is considered an end-of-input assertion.
and the search won't succeed.
True.
I found a solution by first escaping the $ :^ ^
fonction (str, substr) {
See above.
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.
There is not, that would result in a completely different flavor of Regular
Expression syntax called POSIX 1003.2 Basic Regular Expressions (considered
obsolete and only supported for backward compatibility in some programs,
where special characters are to be escaped in order not to be considered
terminal characters.) ECMAScript implementations support only Regular
Expressions modelled after the Regular Expression flavor Perl 5 supports
(but they are not Perl-Compatible Regular Expressions.)
As long as you pass a string as argument to a method that expects
ECMAScript-conforming Regular Expression syntax and want RegExp special
characters be considered terminal characters, you will have to escape them,
either manually or programmatically. (However, if the expression to match
the input string is invariant, you can use RegExp literals; they only
require you to escape the special character once:
new RegExp("a\\$b")
and
/a\$b/
are equivalent _in their value_. See ECMAScript 3, 7.8.5, for the
specification of the differences in implementation.)
While it is possible to create a new RegExp flavor with, for and in
ECMAScript implementations, it is easier to use a method that does not
expect ECMAScript-conforming Regular Expression syntax instead. In
your case, the String.prototype.indexOf() method is sufficient:
if (str.indexOf(substr) > -1)
{
// do something
}
And unless you do something else in your method, it is entirely redundant.
This prototype method is supported in JavaScript since its first version,
and is specified in ECMAScript since its first edition.
HTH
PointedEars
.
- Follow-Ups:
- Re: RegExp and special caracters
- From: Dr John Stockton
- Re: RegExp and special caracters
- References:
- RegExp and special caracters
- From: arno
- RegExp and special caracters
- Prev by Date: RegExp and special caracters
- Next by Date: Re: Array in javascript
- Previous by thread: RegExp and special caracters
- Next by thread: Re: RegExp and special caracters
- Index(es):
Relevant Pages
|