Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
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 6 '11 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? –  sln Feb 6 '11 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. –  tchrist Feb 6 '11 at 17:04

3 Answers 3

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.

share|improve this answer
    
Very nice. Thank you!!! –  James Feb 6 '11 at 23:41

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

share|improve this answer

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

share|improve this answer
    
That is helpful but I just can't get the syntax right. jquery is –  James Feb 6 '11 at 7:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.