Results 1 to 2 of 2

Thread: [RESOLVED] General Regex Question [JS]

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    Resolved [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

  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    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
  •  



Click Here to Expand Forum to Full Width