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

RegExp help!!

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.