regex and g modifier

Discussion in 'Javascript' started by Rolf Kemper, Jul 26, 2004.

  1. Rolf Kemper

    Rolf Kemper Guest

    Dear All,

    may be some one can comment why belows code snipet does not work.(only
    one 'i' item is returned) I tried to use the g modifier in order to
    get an array of values back. (My expection is
    index 0 to 6 where the first index holds the last match according the
    documentations I found)

    My environment is Windows 2000 Server and IE6

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    var out='';
    var reg_sigs = /(i)/g;
    var sigrows = reg_sigs.exec("o,i1&&i2,~i1||~i2,~i2&&i1");
    if (sigrows) {
    for ( i=0; i < sigrows.length ; i++) {
    out = out + "\n" + i + ' ' + sigrows;
    }
    alert(out)
    }
    else {alert('no match')}

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    Is the g modifier only supported for substitutions ? !

    Any help and hint is welcome.
    Rolf
     
    Rolf Kemper, Jul 26, 2004
    #1
    1. Advertising

  2. Rolf Kemper wrote:

    > Dear All,
    >
    > may be some one can comment why belows code snipet does not work.(only
    > one 'i' item is returned) I tried to use the g modifier in order to
    > get an array of values back.


    You need to loop if you use exec and want to find all matches:

    var reg_sigs = /(i)/g;
    var sigrows;
    while (sigrows = reg_sigs.exec("o,i1&&i2,~i1||~i2,~i2&&i1")) {
    var out = '';
    for ( i=0; i < sigrows.length ; i++) {
    out = out + "\n" + i + ' ' + sigrows;
    }
    alert(out)
    }

    Or use the match method of strings e.g.

    var reg_sigs = /(i)/g;
    var sigrows;
    var s = "o,i1&&i2,~i1||~i2,~i2&&i1";
    sigrows = s.match(reg_sigs);
    var out = '';
    for ( i=0; i < sigrows.length ; i++) {
    out = out + "\n" + i + ' ' + sigrows;
    }
    alert(out)

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Jul 26, 2004
    #2
    1. Advertising

  3. Rolf Kemper

    Mick White Guest

    Rolf Kemper wrote:

    > Dear All,
    >
    > may be some one can comment why belows code snipet does not work.(only
    > one 'i' item is returned) I tried to use the g modifier in order to
    > get an array of values back. (My expection is
    > index 0 to 6 where the first index holds the last match according the
    > documentations I found)
    >
    > My environment is Windows 2000 Server and IE6
    >
    > ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    > var out='';
    > var reg_sigs = /(i)/g;
    > var sigrows = reg_sigs.exec("o,i1&&i2,~i1||~i2,~i2&&i1");


    var sigrows = "o,i1&&i2,~i1||~i2,~i2&&i1".match(reg_sigs);

    // String.match() method may be the better choice, the exec method of
    regEx needs to called continually ("for" or "while" loop ) to create the
    Array of matching results.

    Mick

    > if (sigrows) {
    > for ( i=0; i < sigrows.length ; i++) {
    > out = out + "\n" + i + ' ' + sigrows;
    > }
    > alert(out)
    > }
    > else {alert('no match')}
    >
    > ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    >
    > Is the g modifier only supported for substitutions ? !
    >
    > Any help and hint is welcome.
    > Rolf
     
    Mick White, Jul 26, 2004
    #3
    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. maxw_cc
    Replies:
    1
    Views:
    3,159
    Martijn van Steenbergen
    Dec 21, 2003
  2. dost
    Replies:
    5
    Views:
    1,177
    Phlip
    Apr 28, 2006
  3. Replies:
    3
    Views:
    794
    Reedick, Andrew
    Jul 1, 2008
  4. Mark James
    Replies:
    8
    Views:
    165
    David Vallner
    Feb 21, 2006
  5. jOhn
    Replies:
    6
    Views:
    195
    ThoML
    Feb 9, 2008
Loading...

Share This Page