简体   繁体   中英

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#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 .

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 (eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM