0

Possible Duplicate:
How can I match multiple occurrences with a regex in JavaScript similar to PHP’s preg_match_all()?

I am trying to parse an xml document like this:

var str = data.match("<string>" + "(.*?)" + "</string>");
console.log(str);

I want to get all the elements between the [string] in an array but for some reason, it only returns the first string element found. Im not good with regular expressions so Im thinking this is just a small regex issue.

Community
  • 1
  • 1
Farhan Ahmad
  • 5,148
  • 6
  • 40
  • 69

2 Answers2

2

You want it to be global g

var str="<string>1</string><string>2</string><string>3</string>"; 
var n=str.match(/<string>(.*?)<\/string>/g);
 //1,2,3
Sully
  • 14,672
  • 5
  • 54
  • 79
1

You have to form the RegEx adding a g to it like

/Regex/g

pfried
  • 5,000
  • 2
  • 38
  • 71