-1

Sorry if the question was duplicated

I have a XML file, something like

<item>
   <attr1>attr1.1</attr1>
   <attr2>attr1.2</attr2>
</item>
...
<item>
   <attr1>attr2.1</attr1>
   <attr2>attr2.2</attr2>
</item>

And all I want to do is use Regex to get an array of item. For some reason, I don't want to use jQuery here(I have read about jQuery.parseXML). Can you show me how to get all of the items, or something, function in javascript which like match_all() in PHP. And here is my Regex,

 /<item>([\w\W]+?)<\/item>/

Thank you!

simpletron
  • 739
  • 4
  • 14
  • p.s. it's not looking like this is getting much attention, if you think my answers alright you should probably move it onto the accepted stack rather than the outstanding. keep stackoverflow ticking! – kieran Nov 16 '12 at 09:32

2 Answers2

0

As zerkms mentioned there are already ways of parsing XML in javascript that offer a greater range of robustness and flexibility. There is also one particularly infamous answer to the question of attempting to parse supersets of regular languages with regular expressions, in short, it's not the best way to go about this.

However if you are interested in precisely this use case, there is unfortunately no functionality like match_all from php or findall in python's re. Have a look at this stackoverflow, particularly the code block

var str = "I learned to play the Ukulele in Lebanon."
var regex = /le/gi, result, indices = [];
while ( (result = regex.exec(str)) ) {
    indices.push(result.index);
}

In the first answer for some detail on finding many occurrences of the same regex in a similar fashion.

Community
  • 1
  • 1
kieran
  • 1,537
  • 10
  • 10
0

One way to emulate this kind of behaviour is to use a callback function within regex's replace method to build up your results. Here is an example conveniently abstracted into a function...

function match_all(pattern, input){
    var result = [];
    input.replace(pattern, function(_, val){ result.push(val); })
    return result;
}

... check it out with sample xml...

var sample = "<item>\n\
   <attr1>attr1.1</attr1>\n\
   <attr2>attr1.2</attr2>\n\
</item>\n\
...\n\
<item>\n\
   <attr1>attr2.1</attr1>\n\
   <attr2>attr2.2</attr2>\n\
</item>"

console.log(match_all(/<item>([\w\W]+?)<\/item>/gm, sample))
// [
//    "↵   <attr1>attr1.1</attr1>↵   <attr2>attr1.2</attr2>↵",
//    "↵   <attr1>attr2.1</attr1>↵   <attr2>attr2.2</attr2>↵"
// ]
Billy Moon
  • 57,113
  • 24
  • 136
  • 237