471,594 Members | 2,644 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

RegExp help!!

Hi, all!! Need a hand with a regular expression... Let's say I have this
string:
"Now is 'the time' for all 'good men' to come to 'the aid' of their country"

I want to delete each occurrance of any string contained within the
apostrophes, including the apostrophes. So in the above example, the result
should be:
"Now is for all to come to of their country"

Tried several different expressions, but can't find one that works... Any
ideas?

Thanks!
Jack
Jul 20 '05 #1
2 1280
Hi!

Use this script, please:

var s = "Now is 'the time' for all 'good men' to come to 'the aid' of their
country"

s = s.replace(/\'.+?\'/g,'');

alert(s);
--
Sergey.
http://www.takereal.com/freelance/
"Jack Black" <ja*************@hotmail.com> wrote in message
news:3f********@127.0.0.1...
Hi, all!! Need a hand with a regular expression... Let's say I have this
string:
"Now is 'the time' for all 'good men' to come to 'the aid' of their country"
I want to delete each occurrance of any string contained within the
apostrophes, including the apostrophes. So in the above example, the result should be:
"Now is for all to come to of their country"

Tried several different expressions, but can't find one that works... Any
ideas?

Thanks!
Jack

Jul 20 '05 #2
Sergey I.Grachyov wrote:
var s = "Now is 'the time' for all 'good men' to come to 'the aid' of their
country"

s = s.replace(/\'.+?\'/g,'');

alert(s);


Unfortunately, this will not work prior to JavaScript 1.5 where non-greedy
modifiers were implemented at first. Therefore it will probably not work in
Internet Explorer prior to version 6.0 SP-1, Opera below version 7.0 (CMIIW)
and definitely not in Netscape prior to 6.0 (Mozilla prior to /5.0).

However, if you cannot make a RegExp non-greedy, you can still design it
not to match substrings containing specific characters, so

s = s.replace(/'[^']+'/g, '');

will be greedy but will only match what is between each pair of "'",
and not "'the time'...'the aid'". And it will most certainly be
compatible with other UAs than Mozilla/5.0.

BTW: "'" within RegExp literals need not to be escaped.
PointedEars

Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Simon | last post: by
2 posts views Thread by Yang | last post: by
1 post views Thread by ric.castagna | last post: by
2 posts views Thread by Tamas Nyilanszky | last post: by
5 posts views Thread by otrWalter | last post: by
7 posts views Thread by cirfu | last post: by
4 posts views Thread by Andrew Poulos | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

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.