473,468 Members | 1,349 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

RegExp.test

If I test() string two times in a row like:

var re = new RegExp(expression, "gi");
re.test(s1);
re.test(s1);

only the first test passes. Is this normal?

--

Regards,
Karel Miklav
Oct 21 '05 #1
6 1567
Karel Miklav <ka***@inetis.spppambait.com> writes:
If I test() string two times in a row like:

var re = new RegExp(expression, "gi");
re.test(s1);
re.test(s1);

only the first test passes. Is this normal?


Yes.

When you give the "g" option on a regular expression, it is able
to be matched several times against a string, each starting only at
the end of the previous match. Only when the regular expression
fails to match is the starting index reset to zero. It works like
this when calling "exec" on the regular expression, and "test" is
defined in terms of "exec".

Calling "match" on the string instead will do all the matches in one
round, similar to the example below.

Example:
---
function allMatches(re, string) {
var match, result = [];
while((match = re.exec(string))) {
result.push(match[0]);
}
return result;
}

alert(allMatches(/a\w/g,"banananasandapplesauce"));
---

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Oct 21 '05 #2
Lasse Reichstein Nielsen wrote:
When you give the "g" option on a regular expression, it is able
to be matched several times against a string, each starting only at
the end of the previous match. Only when the regular expression
fails to match is the starting index reset to zero. It works like
this when calling "exec" on the regular expression, and "test" is
defined in terms of "exec".


Or you set re.lastIndex to 0 before calling re.test() again.
JW

Oct 21 '05 #3
Janwillem Borleffs wrote:
Lasse Reichstein Nielsen wrote:
Or you set re.lastIndex to 0 before calling re.test() again.


Phew, didn't know about this. Thank you.

--

Karel Miklav
Oct 21 '05 #4
Janwillem Borleffs wrote:
Lasse Reichstein Nielsen wrote:
Or you set re.lastIndex to 0 before calling re.test() again.


Phew, didn't know about this. Thank you.

--

Karel Miklav
Oct 21 '05 #5
Janwillem Borleffs wrote:
Lasse Reichstein Nielsen wrote:
Or you set re.lastIndex to 0 before calling re.test() again.


Phew, didn't know about this. Thank you.

--

Karel Miklav
Oct 21 '05 #6
Janwillem Borleffs wrote:
Lasse Reichstein Nielsen wrote:
Or you set re.lastIndex to 0 before calling re.test() again.


Phew, didn't know about this. Thank you.

--

Karel Miklav
Oct 21 '05 #7

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

Similar topics

10
by: Anand Pillai | last post by:
To search a word in a group of words, say a paragraph or a web page, would a string search or a regexp search be faster? The string search would of course be, if str.find(substr) != -1:...
10
by: Jeff Sandler | last post by:
I have a page that accepts input from many textboxes. Many of the textboxes are intended to accept dates and times, thus, I expect only digits to be entered. I originally tested using parseInt...
0
by: Chris Croughton | last post by:
I'm trying to use the EXSLT regexp package from http://www.exslt.org/regexp/functions/match/index.html (specifically the match function) with the libxml xltproc (which supports EXSLT), but...
8
by: Dmitry Korolyov | last post by:
ASP.NET app using c# and framework version 1.1.4322.573 on a IIS 6.0 web server. A single-line asp:textbox control and regexp validator attached to it. ^\d+$ expression does match an empty...
26
by: Matt Kruse | last post by:
Are there any current browsers that have Javascript support, but not RegExp support? For example, cell phone browsers, blackberrys, or other "minimal" browsers? I know that someone using Netscape...
11
by: HopfZ | last post by:
I coudn't understand some behavior of RegExp.test function. Example html code: ---------------- <html><head></head><body><script type="text/javascript"> var r = /^https?:\/\//g;...
6
by: runsun pan | last post by:
Hi I am wondering why I couldn't get what I want in the following 3 cases of re: (A) var p=/(+-?+):(+)/g p.exec("style='font-size:12'") -- // expected
6
by: =?ISO-8859-1?Q?Une_B=E9v?==?ISO-8859-1?Q?ue?= | last post by:
I wanted to test userAgent by a Regexp the goal : true if UA contains "AppleWebKit/528+ " or "AppleWebKit/525.12+ " note the final "+" false otherwise then i wrote :
2
by: Nathan Sokalski | last post by:
I have the following script that I am using to test some JavaScript RegExp code: function RE() { var testing1=new RegExp("*"); var testing2=new RegExp("{0,}"); var testing3=new RegExp("+");...
4
by: Matt | last post by:
Hello all, I have just discovered (the long way) that using a RegExp object with the 'global' flag set produces inconsistent results when its test() method is executed. I realize that 'global'...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.