[RESOLVED] General Regex Question [JS]
The text has the lines (in this format):
Quote:
Did you know Mary is 15 years old?
Did you know Joe is 57 years old?
Did you know John is 6 years old?
Did you know Melissa is 99 years old?
Here's my pattern:
Code:
Did you know ([A-Za-z]+) is (\d+) years old
Implemented into JavaScript:
Code:
var matched = text.match(/Did you know ([A-Za-z]+) is (\d+) years old/);
The problem is, matched[1] = "Did you know Mary is 15 years old"
It always equals that... How do I get them into an array, where I can loop through? For example, index 0 would have Mary's details, index 1 would have Joe's details, index 2 is John's, and so on...
Thanks guys :afrog:
Re: General Regex Question [JS]
Got it!
Code:
var text = "Did you know Mary is 15 years old?Did you know Joe is 57 years old?Did you know John is 6 years old?Did you know Melissa is 99 years old?";
var rgx = /Did you know ([A-Za-z]+) is (\d+) years old/g;
while((matched = rgx.exec(text)) != null)
{
alert(matched[1] + " - " + matched[2]);
}