Re: RegExp and special caracters



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
.



Relevant Pages

  • Re: RegExp and special caracters
    ... function identifier(str, substr) ... Expression syntax called POSIX 1003.2 Basic Regular Expressions (considered ... where special characters are to be escaped in order not to be considered ... Expressions modelled after the Regular Expression flavor Perl 5 supports ...
    (comp.lang.javascript)
  • Re: Simple RegEx expresion
    ... > I am very new in RegEx and I want to be able to write an expresion that does ... > Return all the characters from a string starting from the 5-th character. ... If you really *wanted* a regular expression for some unknown reason... ... But I have no idea why you'd want that instead of substr(). ...
    (perl.beginners)
  • Re: Regular Expression Function
    ... I want a regular expression to compare sentences and then rate them as ... I have an array with a list of other phrases like so... ... characters will throw things off. ... "In an hour the system will go down for maintenance". ...
    (alt.php)
  • Re: Regular Expression Function
    ... I want a regular expression to compare sentences and then rate them as ... I have an array with a list of other phrases like so.. ... These will be stripped from the input first. ... characters will throw things off. ...
    (alt.php)
  • Re: Expert script (.bat) writers help needed (strip double-quote from string)
    ... Sets or returns the regular expression pattern being searched for. ... Always a RegExp object variable. ... May include any of the regular expression characters defined in the table in the Settings section. ...
    (microsoft.public.windowsxp.help_and_support)