1

Hello I am trying to parse an xml string with js. My string is similar to the one below. I am using .match() function with regular expressions.

var str='<rate val="xyz"></rate> <rate val="klm"></rate> <rate val="mnp"></rate>';
var result=str.match(/val="(.+?)"/g);

I want to get a result array like:

 ["xyz","klm","mnp"]

But it gives me as

["rate val="xyz"","rate val="klm"","rate val="mnp""]

How can I get only values without attribute names?

Ali insan Soyaslan
  • 836
  • 5
  • 14
  • 33

1 Answers1

1

Instead of messing with regex, you could use a combination of filter and map to get the properties of the elements in to an array as you require:

var str = '<rate val="xyz"></rate> <rate val="klm"></rate> <rate val="mnp"></rate>';
var result = $(str).filter('rate').map(function() {
    return $(this).attr('val');    
}).get();

console.log(result);

Example fiddle

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339