|
-
Aug 6th, 2009, 05:19 AM
#1
Thread Starter
Frenzied Member
[RESOLVED] General Regex Question [JS]
The text has the lines (in this format):
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
-
Aug 6th, 2009, 07:29 PM
#2
Thread Starter
Frenzied Member
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]);
}
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|