473,396 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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
Jul 23 '05 #1
2 1519


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

--

Martin Honnen
http://JavaScript.FAQTs.com/

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

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

Is the g modifier only supported for substitutions ? !

Any help and hint is welcome.
Rolf

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: lawrence | last post by:
what is the right modifier to get $string treated as one line in regex? $string = <<<EOD <?php function smoothTalker() { // code here }
1
by: Ivan Marsh | last post by:
Hey Folks, This isn't actually a perl question but since you folks ar the regex experts I thought you might be able to help. I want to do a search and replace in my text editor (that supports...
3
by: Vidar Skjelanger | last post by:
I have a regex for matching VB6-functions, but it hangs on one specific function. The regex: ...
2
by: Allen | last post by:
In regex, ^ and $ shoudl match start/end of a line when the 'm' /multiline modifier is set -- however I just spent the better part of the day trying to figure out why it wasn't working as expected....
7
by: Nightcrawler | last post by:
Hi all, I am trying to use regular expressions to parse out mp3 titles into three different groups (artist, title and remix). I currently have three ways to name a mp3 file: Artist - Title ...
5
by: nel | last post by:
I have two tags: <!--// Remove Begin //--and <!--// Remove End //--> I want to use regi_replace() to remove everything between these tags. The thing is, these tags can be repeated throughout...
4
by: pedrito | last post by:
I have a regex question and it never occurred to me to ask here, until I saw Jesse Houwing's quick response to Phil for his Regex question. I have some filenames that I'm trying to parse out of...
6
by: Schroeder, AJ | last post by:
Hello group, I am attempting to remove double quotes from the beginning and ending of a string. Admittedly, I am not the best with regular expressions, but I do have two that work with...
5
by: Alan M Dunsmuir | last post by:
I am confused by the operation of the RegEx Replace functions provided by PHP. I understand, of course, what is intended if the 'Replace' string is a simple character string, to be slotted into...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.