Re: 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

Re: RegExp and special caracters

From: arno <arno.@no-log.org>
Date: Sat Feb 04 2006 - 19:59:08 CET

Le samedi 04 février 2006, à 12:25:57 +0000, Thomas a écrit :
> 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

Thanx for your complete and useful answer.
str.indexOf was fine for what I had to do.

arno
Received on Tue Feb 7 21:34:19 2006