regular expressions -- very basic grouping question

Discussion in 'Javascript' started by Tom, Nov 15, 2006.

  1. Tom

    Tom Guest

    [abc] - matches "a", "b", or "c"
    [^abc] - matches anything except "a", "b", and "c"
    (abc) - matches "abc", with back reference
    ??? - matches anything except "abc" literally?? What is the syntax for
    this expression?

    If this kind of non-matching grouping expression doesn't exist, what's
    an alternate way to have the regexp engine (perl in my case), match
    "anything but 'abc'"?

    Thanks!
    Tom, Nov 15, 2006
    #1
    1. Advertising

  2. Tom

    Tom Guest

    don't know why I wrote perl -- javascript obviously. working on too
    many things at once!

    Tom wrote:
    > [abc] - matches "a", "b", or "c"
    > [^abc] - matches anything except "a", "b", and "c"
    > (abc) - matches "abc", with back reference
    > ??? - matches anything except "abc" literally?? What is the syntax for
    > this expression?
    >
    > If this kind of non-matching grouping expression doesn't exist, what's
    > an alternate way to have the regexp engine (perl in my case), match
    > "anything but 'abc'"?
    >
    > Thanks!
    Tom, Nov 15, 2006
    #2
    1. Advertising

  3. Tom

    Randy Webb Guest

    Tom said the following on 11/15/2006 2:21 PM:
    > [abc] - matches "a", "b", or "c"
    > [^abc] - matches anything except "a", "b", and "c"
    > (abc) - matches "abc", with back reference
    > ??? - matches anything except "abc" literally?? What is the syntax for
    > this expression?
    >
    > If this kind of non-matching grouping expression doesn't exist, what's
    > an alternate way to have the regexp engine (perl in my case), match
    > "anything but 'abc'"?


    There is probably a RegEx that will do it, but, if all you want is a
    match for abc then you can test with indexOf and if it is present or not
    then act accordingly.

    --
    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 16, 2006
    #3
  4. Tom wrote:

    [snip]
    > [abc] - matches "a", "b", or "c"
    > [^abc] - matches anything except "a", "b", and "c"
    > (abc) - matches "abc", with back reference
    > ??? - matches anything except "abc" literally?? What is the syntax for
    > this expression?

    [/snip]

    Hi

    Your question is ambiguous. Do you mean (a) you only want to accept a
    string as a whole if it does not contain "abc", or (b) you only want
    your pattern to accept an "a" if it is not followed by "bc", but it
    does not matter if "abc" appears somewhere else?

    If the answer is (a), then as already noted, you could use indexOf
    instead.

    If the answer is (b), then one way is to use a negative lookahead.

    To quote from Microsoft's JScript documentation:-

    (?!pattern)
    "Negative lookahead matches the search string at any point where a
    string not matching pattern begins. This is a non-capturing match, that
    is, the match is not captured for possible later use. For example
    'Windows (?!95|98|NT|2000)' matches "Windows" in "Windows 3.1" but does
    not match "Windows" in "Windows 2000". Lookaheads do not consume
    characters, that is, after a match occurs, the search for the next
    match begins immediately following the last match, not after the
    characters that comprised the lookahead."

    Example:-

    var re = /a(?!bc)/gi
    var s = "adefgiabc";
    alert(re.exec(s).length);

    alerts 1, not 2.

    Note that (?!pattern) is not supported by some older IE browsers.

    Regards

    Julian Turner
    Julian Turner, Nov 16, 2006
    #4
  5. In message <>, Wed,
    15 Nov 2006 11:21:09, Tom <> writes
    >[abc] - matches "a", "b", or "c"
    >[^abc] - matches anything except "a", "b", and "c"
    >(abc) - matches "abc", with back reference
    >??? - matches anything except "abc" literally?? What is the syntax for
    >this expression?
    >
    >If this kind of non-matching grouping expression doesn't exist, what's
    >an alternate way to have the regexp engine (perl in my case), match
    >"anything but 'abc'"?


    I am using a standards-compliant newsreader. Your posting agent does
    horrible things to your layout, as shown when viewing your article in my
    reader; however, in reply mode I get the material quoted as presumably
    intended. Please change your /modus operandi/ appropriately.

    [abc] matches any character in the set "abc".
    [^abc] matches any character not in the set "abc".

    One approach to your problem, which should work even in older
    RegExp-capable browsers, would be to use .replace(/abc/g, "\u0416"),
    where \u0416 is a character which is known not to appear in the original
    string, and you can then use [^\u0416] to be not matched.

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

    --
    (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
    <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.
    Dr J R Stockton, Nov 16, 2006
    #5
  6. Tom

    Randy Webb Guest

    Dr J R Stockton said the following on 11/16/2006 9:29 AM:
    > In message <>, Wed,
    > 15 Nov 2006 11:21:09, Tom <> writes
    >> [abc] - matches "a", "b", or "c"
    >> [^abc] - matches anything except "a", "b", and "c"
    >> (abc) - matches "abc", with back reference
    >> ??? - matches anything except "abc" literally?? What is the syntax for
    >> this expression?
    >>
    >> If this kind of non-matching grouping expression doesn't exist, what's
    >> an alternate way to have the regexp engine (perl in my case), match
    >> "anything but 'abc'"?

    >
    > I am using a standards-compliant newsreader. Your posting agent does
    > horrible things to your layout, as shown when viewing your article in my
    > reader; however, in reply mode I get the material quoted as presumably
    > intended. Please change your /modus operandi/ appropriately.


    Sounds like it is something with your newsreader. I have read the
    original using three different newsreaders and none of them screw up the
    layout.

    --
    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 16, 2006
    #6
    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. Raymond Arthur St. Marie II of III

    very Very VERY dumb Question About The new Set( ) 's

    Raymond Arthur St. Marie II of III, Jul 23, 2003, in forum: Python
    Replies:
    4
    Views:
    456
    Raymond Hettinger
    Jul 27, 2003
  2. ds2000
    Replies:
    2
    Views:
    404
    Jonathan Gardner
    May 24, 2004
  3. Peter

    Very very very basic question

    Peter, Feb 8, 2005, in forum: C Programming
    Replies:
    14
    Views:
    506
    Dave Thompson
    Feb 14, 2005
  4. Will
    Replies:
    15
    Views:
    173
    Anno Siegel
    Apr 11, 2005
  5. Noman Shapiro
    Replies:
    0
    Views:
    222
    Noman Shapiro
    Jul 17, 2013
Loading...

Share This Page