regex and g modifier

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rolf Kemper

    regex and g modifier

    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[i];
    }
    alert(out)
    }
    else {alert('no match')}

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

    Is the g modifier only supported for substitutions ? !

    Any help and hint is welcome.
    Rolf
  • Martin Honnen

    #2
    Re: regex and g modifier



    Rolf Kemper wrote:
    [color=blue]
    > 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.[/color]

    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[i];
    }
    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_sig s);
    var out = '';
    for ( i=0; i < sigrows.length ; i++) {
    out = out + "\n" + i + ' ' + sigrows[i];
    }
    alert(out)

    --

    Martin Honnen


    Comment

    • Mick White

      #3
      Re: regex and g modifier

      Rolf Kemper wrote:
      [color=blue]
      > 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");[/color]

      var sigrows = "o,i1&&i2,~i1|| ~i2,~i2&&i1".ma tch(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
      [color=blue]
      > if (sigrows) {
      > for ( i=0; i < sigrows.length ; i++) {
      > out = out + "\n" + i + ' ' + sigrows[i];
      > }
      > alert(out)
      > }
      > else {alert('no match')}
      >
      > +++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++++++++ ++++++++++++++
      >
      > Is the g modifier only supported for substitutions ? !
      >
      > Any help and hint is welcome.
      > Rolf[/color]

      Comment

      Working...