RegExp and special caracters

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
 
T

Thomas 'PointedEars' Lahn

arno said:
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
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Sat, 4 Feb
2006 13:25:57 remote, seen in Thomas
'PointedEars' Lahn said:
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.
 
T

Thomas 'PointedEars' Lahn

Dr said:
[...] Thomas 'PointedEars' Lahn [...] posted :
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
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Sun, 5 Feb
2006 18:34:20 remote, seen in Thomas
'PointedEars' Lahn said:
Dr said:
[...] 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.
 
T

Thomas 'PointedEars' Lahn

Dr said:
[...] Thomas 'PointedEars' Lahn [...] posted :
Dr said:
[...] 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
 
R

Randy Webb

Thomas 'PointedEars' Lahn said the following on 2/6/2006 2:14 PM:
Dr said:
[...] 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...n+Stockton+tab+indent&rnum=2#94047cd3bbbf9a35
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.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Mon, 6 Feb
2006 20:14:52 remote, seen in Thomas
'PointedEars' Lahn said:
Dr said:
[...] 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top