Forum Moderators: open
var re = new RegExp(document.t.regex.value);
var m = re.exec(document.t.myString.value);
if (m == null) {
alert("No match");
} else {
alert("No of matches= "+m.length);
var s = "Match at position " + m.index + ":\n";
for (i = 0; i < m.length; i++) {
s = s + m[i] + "\n";
alert(m[i] );
}
//alert(s);
}
input string consist of two HTML links but its not returning all links. where am I doing wrong?
var m = document.t.myString.value.match( re);
Firstly, I believe you have to specify the 'g' modifier to make it match more than once, like this:
var re = new RegExp( document.t.regex.value, "g");
And you are assuming that m[...] refers to the match occurance, however that refers to the strings captured in the ()'s in your regex, if any.
Not sure of the global match syntax in JS. In Perl you'd do a while loop and keep calling match.
It would be helpful to see your regex, I see you have it in a form, is this just so you can mess with it on the fly? Maybe you could give me an example of the string you're trying to match on?
<a.+?href="(.*?)"
Instead of matching [^"] (not a quote), matching any character until the next quote is easier.
I find this tool absolute gold dust as I use Perl a lot and you can't use Perl without a lot of Regexing.
[weitz.de...]
Anyways, I can't see what's wrong if you're using the "g", although it was not present in your example code. I would try repeatedly calling .match until you get a null return.