1

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?

3
  • 2
    Jamie Zawinski, 1997 : "Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems." ... :) Commented Feb 6, 2011 at 6:47
  • What part stays the same in the context of atrib/val? What about google.com/g/...#mobile? Any other attr/val's in that tag? Is this in the context of xml? Commented Feb 6, 2011 at 7:04
  • Yes, of course it can be done with regexes. However, Javascript’s regex implementation is not very powerful; it cannot even cope with Unicode correctly. Still, this may give you some ideas. Commented Feb 6, 2011 at 17:04

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

That is helpful but I just can't get the syntax right. jquery is
0

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

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.