By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,883 Members | 1,952 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,883 IT Pros & Developers. It's quick & easy.

RegExp and special caracters

P: n/a
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
Feb 4 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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
Feb 4 '06 #2

P: n/a
JRS: In article <21****************@PointedEars.de>, dated Sat, 4 Feb
2006 13:25:57 remote, seen in news:comp.lang.javascript, Thomas
'PointedEars' Lahn <Po*********@web.de> posted :
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.


Tab is perfectly acceptable in News, provided that the lines do not
become long enough to line-wrap.

A newsreader should render them as at least one space, then more if
required to reach the next tab stop. Tab stops should by initial
default be at columns 8N+1, and the reader should be able to alter tab
settings.

If you are implying that your system ignores tabs, then either you are a
damn fool for choosing to use software that cannot do it correctly, or
an awkward sod for choosing not to display them.

Granted, we do already know that you are the latter, and it would not be
at all surprising to learn that you've specifically chosen to make such
a setting merely to give yourself a "cause" for complaints that you
might otherwise have omitted to make.

There should be no need to remind a French poster of how obnoxious
Germans can be; they still remember 1940, 1914, 1870, and even 1415 and
1346.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME
Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html> -> Timo Salmi: Usenet Q&A.
Web <URL:http://www.merlyn.demon.co.uk/news-use.htm> : about usage of News.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
Feb 5 '06 #3

P: n/a
Dr John Stockton wrote:
[...] Thomas 'PointedEars' Lahn [...] posted :
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.


Tab is perfectly acceptable in News, provided that the lines do not
become long enough to line-wrap.


Nonsense.
A newsreader should render them as at least one space, then more if
required to reach the next tab stop. [...]


The position of the next tab stop is inconsistent among platforms,
operating systems, display devices and programs.
PointedEars
Feb 5 '06 #4

P: n/a
JRS: In article <55****************@PointedEars.de>, dated Sun, 5 Feb
2006 18:34:20 remote, seen in news:comp.lang.javascript, Thomas
'PointedEars' Lahn <Po*********@web.de> posted :
Dr John Stockton wrote:
[...] Thomas 'PointedEars' Lahn [...] posted :
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.


Tab is perfectly acceptable in News, provided that the lines do not
become long enough to line-wrap.


Nonsense.
A newsreader should render them as at least one space, then more if
required to reach the next tab stop. [...]


The position of the next tab stop is inconsistent among platforms,
operating systems, display devices and programs.


Certainly; the reader is thus enabled to choose what he prefers. Only a
control-freak would insist on anything else.

Some readers like to see code indented in 8-space units; some prefer two
or three spaces for indentation. There is no reason why they should not
be allowed to exercise that preference, though there is no convention
that an author should provide that choice.

The only rational objection to the use of tabs for indentation comes
when the use of a wide indentation rather than, say, a two-space
indentation leads either to line-breaking in the sending agent or to
lines which are unnecessarily inconvenient to read.

Apologies for omitting 1815 from my list of dates.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME
Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html> -> Timo Salmi: Usenet Q&A.
Web <URL:http://www.merlyn.demon.co.uk/news-use.htm> : about usage of News.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
Feb 6 '06 #5

P: n/a
Dr John Stockton wrote:
[...] Thomas 'PointedEars' Lahn [...] posted :
Dr John Stockton wrote:
[...] Thomas 'PointedEars' Lahn [...] posted :
> 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.
Tab is perfectly acceptable in News, provided that the lines do not
become long enough to line-wrap.

Nonsense.
A newsreader should render them as at least one space, then more if
required to reach the next tab stop. [...]

The position of the next tab stop is inconsistent among platforms,
operating systems, display devices and programs.


Certainly; the reader is thus enabled to choose what he prefers. Only a
control-freak would insist on anything else. [...]


Since it has also been established with you now that the next tab stop may
appear anywhere depending on any or all of the factors named, and therefore
the display of tab-indented code certainly is inconsistent among readers
while display of space-indented code certainly is not, it all boils down to
the major rule of thumb for USENET: People who wish their postings to be
read (and their source code to be peer-reviewed) should accommodate the
needs of their potential readers, period.

BTW, I would appreciate it if you contributed more to this newsgroup than
just such meta-discussions, FAQ references and flames.
PointedEars
Feb 6 '06 #6

P: n/a
Thomas 'PointedEars' Lahn said the following on 2/6/2006 2:14 PM:
Dr John Stockton wrote:
[...] Thomas 'PointedEars' Lahn [...] posted :
Dr John Stockton wrote:
[...] Thomas 'PointedEars' Lahn [...] posted :
>> 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.
Tab is perfectly acceptable in News, provided that the lines do not
become long enough to line-wrap.
Nonsense.
A newsreader should render them as at least one space, then more if
required to reach the next tab stop. [...]
The position of the next tab stop is inconsistent among platforms,
operating systems, display devices and programs. Certainly; the reader is thus enabled to choose what he prefers. Only a
control-freak would insist on anything else. [...]


Since it has also been established with you now that the next tab stop may
appear anywhere depending on any or all of the factors named, and therefore
the display of tab-indented code certainly is inconsistent among readers
while display of space-indented code certainly is not, it all boils down to
the major rule of thumb for USENET: People who wish their postings to be
read (and their source code to be peer-reviewed) should accommodate the
needs of their potential readers, period.


You would have done better by JRS to have pointed to a thread such as
this one:

<URL:
http://groups.google.com/group/comp....047cd3bbbf9a35
Where he says:
<quote>
It's helpful to use two spaces per indent level, rather than a tab; an
editor can easily make the change.
</quote>

But the difference between his post and yours is that he makes it a
request, you make it something that should be forced on people. The
difference, and perception by people reading it, is huge.
BTW, I would appreciate it if you contributed more to this newsgroup than
just such meta-discussions, FAQ references and flames.


Who the hell died and left you in charge of what people contribute to
this NG? As many disagreements as I have had with JRS over the years the
one thing I can definitely say is that this NG is better off with him
than without him. I can not say the same for you though.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Feb 7 '06 #7

P: n/a
JRS: In article <14****************@PointedEars.de>, dated Mon, 6 Feb
2006 20:14:52 remote, seen in news:comp.lang.javascript, Thomas
'PointedEars' Lahn <Po*********@web.de> posted :
Dr John Stockton wrote:
[...] Thomas 'PointedEars' Lahn [...] posted :
Dr John Stockton wrote:
[...] Thomas 'PointedEars' Lahn [...] posted :
>> 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.
Tab is perfectly acceptable in News, provided that the lines do not
become long enough to line-wrap.
Nonsense.
A newsreader should render them as at least one space, then more if
required to reach the next tab stop. [...]
The position of the next tab stop is inconsistent among platforms,
operating systems, display devices and programs.
Certainly; the reader is thus enabled to choose what he prefers. Only a
control-freak would insist on anything else. [...]


Since it has also been established with you now that the next tab stop may
appear anywhere depending on any or all of the factors named, and therefore
the display of tab-indented code certainly is inconsistent among readers
while display of space-indented code certainly is not, it all boils down to
the major rule of thumb for USENET: People who wish their postings to be
read (and their source code to be peer-reviewed) should accommodate the
needs of their potential readers, period.


Indeed. That can be done by indenting either with tabs or with spaces.
If it is done with tabs then the reader can select what best fits his
needs or wishes.

Another step to accommodate the needs of the variety of readers would be
for you to use attribution and signature conformant with Usenet norms.

BTW, I would appreciate it if you contributed more to this newsgroup than
just such meta-discussions, FAQ references and flames.


Heil Hypocrite Thomas! If you desist from intestinal retrostalsis and
bullying, you will not need to be the subject of persistent criticism.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Feb 7 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.