regex and g modifier

R

Rolf Kemper

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
 
M

Martin Honnen

Rolf said:
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)
 
M

Mick White

Rolf said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top