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. Advertisements

  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. Advertisements

  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. Advertisements

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,375
    Martijn van Steenbergen
    Dec 21, 2003
  2. Xah Lee
    Replies:
    1
    Views:
    1,171
    Ilias Lazaridis
    Sep 22, 2006
  3. dost
    Replies:
    5
    Views:
    1,371
    Phlip
    Apr 28, 2006
  4. Replies:
    3
    Views:
    1,097
    Reedick, Andrew
    Jul 1, 2008
  5. Mark James
    Replies:
    8
    Views:
    279
    David Vallner
    Feb 21, 2006
  6. jOhn
    Replies:
    6
    Views:
    338
    ThoML
    Feb 9, 2008
  7. Mahesh Asolkar

    Prematch ($`) and the m//g modifier

    Mahesh Asolkar, Mar 10, 2006, in forum: Perl Misc
    Replies:
    7
    Views:
    184
    John W. Krahn
    Mar 10, 2006
  8. Replies:
    2
    Views:
    583
Loading...