473,385 Members | 1,396 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

RegExp and special caracters

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
7 1883
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Psykotik | last post by:
Hello, I've two problems with php. My spec : Windows XP SP1, KFWS 2.2.0, IE 6 SP1, PHP 4.3.3. Web server is "KF web server", a server very very user friendly (HTML interface), and which...
10
by: Anand Pillai | last post by:
To search a word in a group of words, say a paragraph or a web page, would a string search or a regexp search be faster? The string search would of course be, if str.find(substr) != -1:...
1
by: Matthias HALDIMANN | last post by:
When importing data into MS SQL Server 2000 from a MyODBC (v3.51) data source using Data Transformation Services, special characters like öäüéàè are not imported correctly. However, when the MyODBC...
5
by: Syed Ali | last post by:
Hello, I am trying to create a regexp to express non letters and space. I tried using: var myreg = new RegExp (""); However, it is not working. Basically I want to allow only words with...
4
by: Ricardo Jesus | last post by:
I'm reading an XML document and creating a CMS Posting for eacho specific node but i'm trying to save the posting using the title as Posting.Name. For this to work i have to remove all special...
7
by: Mindy Geac | last post by:
Is it possible to delete special caracters from a string and multiple spaces? When the input is : "a&*bbb cc/c d!d" I want the result : "abbb ccc dd" thnx. MJ <?php
1
by: Fabiano | last post by:
Please, i got an XML String that contains some special caracters like linefeed, tab and others. How can i replace this chars? Can i use an ASCII value at the XXX.replace() method? Tks in...
2
by: alain.hogue | last post by:
I have a table "tblCards" with a field "Titre" that contain french words (Hébert, Gagné, Hélène, etc....) in an database with SQLEXPRESS 2005. I have used the asp:SqlDataSource to filter the...
6
by: Christoph | last post by:
I'm trying to set up client side validation for a textarea form element to ensure that the data entered does not exceed 200 characters. I'm using the following code but it doesn't seem to be...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.