how to match this regexp?

Discussion in 'Javascript' started by shellon, Nov 24, 2006.

  1. shellon

    shellon Guest

    Hi all:
    I want to match a string that not ended with the string
    "welcome", how do write the RegExp? Appreciate your help!
     
    shellon, Nov 24, 2006
    #1
    1. Advertising

  2. shellon

    Randy Webb Guest

    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/
     
    Randy Webb, Nov 24, 2006
    #2
    1. Advertising

  3. 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
     
    Bart Van der Donck, Nov 24, 2006
    #3
  4. shellon

    Jonas Raoni Guest

    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
     
    Jonas Raoni, Nov 24, 2006
    #4
  5. shellon

    Jonas Raoni Guest

    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
     
    Jonas Raoni, Nov 24, 2006
    #5
  6. shellon

    mick white Guest

    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
     
    mick white, Nov 24, 2006
    #6
  7. shellon

    Jonas Raoni Guest

    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
     
    Jonas Raoni, Nov 24, 2006
    #7
  8. shellon

    mick white Guest

    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
    >
    >
     
    mick white, Nov 24, 2006
    #8
  9. shellon

    shellon Guest

    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
    > >
    > >
     
    shellon, Nov 26, 2006
    #9
  10. In comp.lang.javascript message
    <>, Sat, 25 Nov 2006
    19:08:32, shellon <> wrote:
    >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.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr J R Stockton, Nov 26, 2006
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hiwa
    Replies:
    0
    Views:
    648
  2. Mikel Lindsaar
    Replies:
    0
    Views:
    510
    Mikel Lindsaar
    Mar 31, 2008
  3. Old Echo
    Replies:
    1
    Views:
    187
    Adam Shelly
    Sep 4, 2008
  4. Joao Silva
    Replies:
    16
    Views:
    381
    7stud --
    Aug 21, 2009
  5. Uldis  Bojars
    Replies:
    2
    Views:
    196
    Janwillem Borleffs
    Dec 17, 2006
Loading...

Share This Page