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

how to match this regexp?

P: n/a
Hi all:
I want to match a string that not ended with the string
"welcome", how do write the RegExp? Appreciate your help!

Nov 24 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
shellon said the following on 11/24/2006 5:32 AM:
Hi all:
I want to match a string that not ended with the string
"welcome", how do write the RegExp? Appreciate your help!
var string1 = "some string with welcome but not at the end";
var string2 = "some string with welcome and also at the end welcome";
var test = string2.substring(string2.length,string2.length-7)
if (test == 'welcome')
{alert('It has welcome at the end')}
else
{alert('It does not have welcome at the end')}

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 24 '06 #2

P: n/a
Randy Webb escreveu:
shellon said the following on 11/24/2006 5:32 AM:
>Hi all:
I want to match a string that not ended with the string
"welcome", how do write the RegExp? Appreciate your help!

var string1 = "some string with welcome but not at the end";
var string2 = "some string with welcome and also at the end welcome";
var test = string2.substring(string2.length,string2.length-7)
This way it looks cutier :)

alert("lala welcome".substr(-7).toLowerCase() == "welcome");

But I preffer using the regexp, it's more compact :b

alert(/welcome$/i.test("lala welcome"));
Jonas Raoni Soares Silva
-------------------------
http://www.jsfromhell.com
Nov 24 '06 #3

P: n/a
Jonas Raoni wrote:
shellon said the following on 11/24/2006 5:32 AM:
I want to match a string that not ended with the string
"welcome", how do write the RegExp?
alert("lala welcome".substr(-7).toLowerCase() == "welcome");
But I preffer using the regexp, it's more compact :b
alert(/welcome$/i.test("lala welcome"));
I prefer that too, because the former would display "false" in MSIE6
and "true" in FF1.

--
Bart

Nov 24 '06 #4

P: n/a
Bart Van der Donck escreveu:
Jonas Raoni wrote:
>alert("lala welcome".substr(-7).toLowerCase() == "welcome");
But I preffer using the regexp, it's more compact :b
alert(/welcome$/i.test("lala welcome"));

I prefer that too, because the former would display "false" in MSIE6
and "true" in FF1.
It's true, I forgot that IE was different about substr, this works on
both :]

alert("lala welcome".slice(-7).toLowerCase() == "welcome");
Jonas Raoni Soares Silva
-------------------------
http://www.jsfromhell.com
Nov 24 '06 #5

P: n/a
Jonas Raoni wrote:

>
But I preffer using the regexp, it's more compact :b

alert(/welcome$/i.test("lala welcome"));
Of course the word "welcome" is likely to followed by a full stop ("."),
or a space(s) in a real world situation.

/welcome\.?\s?$/i

Something like that, not tested.

Mick
>

Jonas Raoni Soares Silva
-------------------------
http://www.jsfromhell.com
Nov 24 '06 #6

P: n/a
mick white escreveu:
Jonas Raoni wrote:
Of course the word "welcome" is likely to followed by a full stop ("."),
or a space(s) in a real world situation.
Yes :]
/welcome\.?\s?$/i
Maybe the "\b" will fit better :]
--
Jonas Raoni Soares Silva
http://www.jsfromhell.com
Nov 24 '06 #7

P: n/a
Jonas Raoni wrote:
mick white escreveu:
>Jonas Raoni wrote:
Of course the word "welcome" is likely to followed by a full stop
("."), or a space(s) in a real world situation.


Yes :]
>/welcome\.?\s?$/i


Maybe the "\b" will fit better :]

I concur...
Mick
>
Nov 24 '06 #8

P: n/a
Thanks a lot! I'm sorry that I didn't describe my problem exactly, The
pattern I want to match is actually is :

I want to match all the pages of the site www.raphsy.com but the home
page www.raphsy.com/welcome.html

so one Regexp maybe: str.match(/^www\.raphsy\.com\//) &&
!str.match(/welcome\.html$/)

but I don't want to use two bool expression, I want to combine the two
expression in one RegExp, that's to say: how to negate a pattern(like
/welcome\.html/) in RegExp?

Appreciate your help!!

"mick white д
"
Jonas Raoni wrote:
mick white escreveu:
Jonas Raoni wrote:
Of course the word "welcome" is likely to followed by a full stop
("."), or a space(s) in a real world situation.

Yes :]
/welcome\.?\s?$/i

Maybe the "\b" will fit better :]

I concur...
Mick
Nov 26 '06 #9

P: n/a
In comp.lang.javascript message
<11**********************@14g2000cws.googlegroups. com>, Sat, 25 Nov 2006
19:08:32, shellon <zh*********@gmail.comwrote:
>Thanks a lot! I'm sorry that I didn't describe my problem exactly, The
pattern I want to match is actually is :

I want to match all the pages of the site www.raphsy.com but the home
page www.raphsy.com/welcome.html

so one Regexp maybe: str.match(/^www\.raphsy\.com\//) &&
!str.match(/welcome\.html$/)
That rejects, contrary to specification,
www.raphsy.com/banana/welcome.html
>but I don't want to use two bool expression, I want to combine the two
expression in one RegExp, that's to say: how to negate a pattern(like
/welcome\.html/) in RegExp?
And does it have to be compatible with all browsers that understand
RegExps? If so, consider

OK = !!str.replace(/welcome.html/, "\u20A4")
.match(/^www\.raphsy\.com\/[^\u20A4]/)

Few modern pages will have \u20A4 anywhere, let alone in the URL.

It's a good idea to read the newsgroup and its old FAQ. See below.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
<URL:http://www.jibbering.com/faq/ Old RC FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Nov 26 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.