开发者

RegEx to parse complex xml javascript

I am working in a restricted Javascript environment and don't have an xml parser or dom access.

The format goes like this:

<gd:phoneNumber rel="http://schemas.google.com/g/2005开发者_Python百科#mobile">206 555 1212</gd:phoneNumber>

I need to get string[] value: mobile, 206 555 1212

The values will be different every time but the tags always the same.

Then I need to be able to replace the values for example: home, 555-555-5555

Can this be done in regEx?


There is fast-xml-parser which is based on regex only. You can include that in your project.

//var xml2json = require('fast-xml-parser').parse;
var jsonObj = xml2json('<gd:phoneNumber rel="http://schemas.google.com/g/2005#mobile">206 555 1212</gd:phoneNumber>', {ignoreNameSpace : true});
console.log(jsonObj.phoneNumber); // "206 555 1212"

Or if you make the regex yourself, I'll suggest you to use regex to capture matching string as @DaveWard suggested in his answer instead of using replace.


take a look at http://www.webreference.com/js/column5/methods.html


This is what I have so far and it works but is there a better way?

"<gd:phoneNumber rel=http://schemas.google.com/g/2005#mobile>206 555 1212</gd:phoneNumber>".replace(/#.*</g, '#home>111-111-1111<')

Returns:

"<gd:phoneNumber rel=http://schemas.google.com/g/2005#home>111-111-1111</gd:phoneNumber>"

So I can inject the new values

"<gd:phoneNumber rel=http://schemas.google.com/g/2005#mobile>206 555 1212</gd:phoneNumber>".match(/#.*</g)[0].replace(/[#<]/g, "").split(/>/)

returns: ["mobile", "206 555 1212"]

allowing me to get the values


This retrieves the matches and performs replacements:

var testString = '<gd:phoneNumber rel=http://schemas.google.com/g/2005#mobile>206 555 1212</gd:phoneNumber>';

var regex = /.*#(\w+)">(.*)</i;

// matches[1] will be "mobile" and matches[2] will be "206 555 1212"
var matches = regex.exec(testString);

// Replace #mobile with #home
testString = testString.replace(matches[1], 'home');

// Replace the phone number with 555 555 5555
testString = testString.replace(matches[2], '555 555 5555');

Those simple replacements will work as long as there's no overlap between those values and the rest of the XML element's contents (e.g. if the schemas.google.com URL contained the string mobile somewhere before #mobile, this wouldn't work). Long as that's the case, this is the easier way to do the replacements.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜