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?

James
  • 11
  • 1
  • 2
  • 2
    Jamie Zawinski, 1997 : "Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems." ... :) – crowne Feb 06 '11 at 06: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? –  Feb 06 '11 at 07: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](http://stackoverflow.com/questions/4284176/doubt-in-parsing-data-in-perl-where-am-i-going-wrong/4286326#4286326) may give you some ideas. – tchrist Feb 06 '11 at 17:04

4 Answers4

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.

Amit Kumar Gupta
  • 7,193
  • 12
  • 64
  • 90
0

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

Ehsan
  • 1,662
  • 6
  • 28
  • 49
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

James
  • 11
  • 1
  • 2
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.

Dave Ward
  • 59,815
  • 13
  • 117
  • 134