Karel Miklav said:
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"));