regular expressions and multiple match

Discussion in 'Javascript' started by ast, Mar 11, 2011.

  1. ast

    ast Guest

    hi,

    let us search a.+b in string abcabcabc

    var reg = /a.+b/g;
    var tab = reg.exec("abcabcabc");

    document.write(tab[0]);

    there are 2 possible matchs:

    abcab
    abcabcab

    javascript choose the second one. Why not. But what is the rule ?
    Does javascript always provide the longest possible match ?

    If i want abcab, is there something to do ?
    ast, Mar 11, 2011
    #1
    1. Advertising

  2. ast wrote:

    > let us search a.+b in string abcabcabc
    >
    > var reg = /a.+b/g;
    > var tab = reg.exec("abcabcabc");
    >
    > document.write(tab[0]);
    >
    > there are 2 possible matchs:
    >
    > abcab
    > abcabcab
    >
    > javascript choose the second one. Why not. But what is the rule ?
    > Does javascript always provide the longest possible match ?


    (There is no "javascript".)

    Yes, RTFM. Regular expressions are "greedy" by default.

    > If i want abcab, is there something to do ?


    RTFM.


    PointedEars
    --
    Anyone who slaps a 'this page is best viewed with Browser X' label on
    a Web page appears to be yearning for the bad old days, before the Web,
    when you had very little chance of reading a document written on another
    computer, another word processor, or another network. -- Tim Berners-Lee
    Thomas 'PointedEars' Lahn, Mar 11, 2011
    #2
    1. Advertising

  3. ast wrote:

    > let us search a.+b in string abcabcabc
    > var reg = /a.+b/g;
    > var tab = reg.exec("abcabcabc");
    > document.write(tab[0]);
    >
    > there are 2 possible matchs:
    > abcab
    > abcabcab
    > javascript choose the second one. Why not. But what is the rule ?
    > Does javascript always provide the longest possible match ?


    The rule is to return the (chronologically) last found match.

    > If i want abcab, is there something to do ?


    '+' = repeat 1 or more times
    '+?' = repeat 1 or more times, as few as possible

    So:

    var reg = /a.+?b/g;

    Hope this helps,

    --
    Bart
    Bart Van der Donck, Mar 11, 2011
    #3
  4. Bart Van der Donck wrote:

    > ast wrote:
    >> var reg = /a.+b/g;
    >> var tab = reg.exec("abcabcabc");
    >> Does javascript always provide the longest possible match ?

    >
    > The rule is to return the (chronologically) last found match.


    Sorry, incorrect. It's indeed the longest one.

    --
    Bart
    Bart Van der Donck, Mar 11, 2011
    #4
  5. "ast" <> writes:

    > hi,
    >
    > let us search a.+b in string abcabcabc
    >
    > var reg = /a.+b/g;
    > var tab = reg.exec("abcabcabc");
    > document.write(tab[0]);
    >
    > there are 2 possible matchs:
    >
    > abcab
    > abcabcab
    >
    > javascript choose the second one. Why not. But what is the rule ?


    The rule is that the "+" quantifier means: Match as many as possible
    of the regexp it's quantifying (in this case '.').

    The "as many as possible" doesn't always mean "as long as possible",
    e.g.,
    /(?:..|...)+/.exec("123456789")
    matches only "12345678", not all nine characters.

    > Does javascript always provide the longest possible match ?


    For the "+" quantifier, yes.

    > If i want abcab, is there something to do ?


    Use the "+?" (non-greedy) quantifier:
    /a.+?b/

    /L
    --
    Lasse Reichstein Holst Nielsen
    'Javascript frameworks is a disruptive technology'
    Lasse Reichstein Nielsen, Mar 11, 2011
    #5
    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. Jay Douglas
    Replies:
    0
    Views:
    598
    Jay Douglas
    Aug 15, 2003
  2. mikko.n

    Regular expressions (multiple match problem)

    mikko.n, Apr 2, 2008, in forum: C Programming
    Replies:
    5
    Views:
    473
    Antoninus Twink
    Apr 2, 2008
  3. Carl Cunningham

    Regular expressions and parenthesis in match text

    Carl Cunningham, Sep 15, 2003, in forum: Perl Misc
    Replies:
    2
    Views:
    113
    Carl Cunningham
    Sep 15, 2003
  4. ast
    Replies:
    2
    Views:
    117
  5. Noman Shapiro
    Replies:
    0
    Views:
    232
    Noman Shapiro
    Jul 17, 2013
Loading...

Share This Page