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.

Given a string like:

</gd:organization><gd:email rel='http://schemas.google.com/g/2005#other' address='[email protected]'/><gd:email rel='http://schemas.google.com/g/2005#other' address='[email protected]' primary='true'/><gd:phoneNumber rel='http://schemas.google.com/g/2005#work'>011 360 51 60</gd:phoneNumber>

I need to remove from the string:

<gd:email rel='http://schemas.google.com/g/2005#other' address='[email protected]'/>

- based on a match to: [email protected].

It has to be done in basic JavaScript and I can't import any special parsing tools. I can't seem to find a combination that does not error out.

Thank you!

share|improve this question
 
Nowhere near enough information - you're asking for a general RegEx but provide only one example input/output. Tell us the characteristics of what it is you need to remove. –  Madbreaks Feb 2 '13 at 0:18
 
I get these xml strings that contain contact information from Google and need to remove certain elements containing a certain email address. –  James Ferreira Feb 2 '13 at 0:20
 
Sorry I had not added the full code. –  James Ferreira Feb 2 '13 at 0:25
 
This doesn't look like valid XML (starts with a close tag), if it was you could use DOMParser. RegExp isn't designed to parse stuff like this. –  Paul S. Feb 2 '13 at 0:27
 
If only I could but I need to do it in Google Apps Script which means no DOMParser and there is no built in methods to handle XML. Yes I only posted part of the string. –  James Ferreira Feb 2 '13 at 0:30
show 1 more comment

2 Answers

up vote 0 down vote accepted

If this must be done in RegExp, you could attempt something like the following only if you know that all <gd:email> parts will have an address attribute, quotes are ' and end /> without address=' or /> appearing inside any values on those attributes.

"</gd:organization><gd:email rel='http://schemas.google.com/g/2005#other' address='[email protected]'/><gd:email rel='http://schemas.google.com/g/2005#other' address='[email protected]' primary='true'/><gd:phoneNumber rel='http://schemas.google.com/g/2005#work'>011 360 51 60</gd:phoneNumber>"
.replace(
    /<gd:email .*?address='([^']*)'.*?\/>/g, // match email node
    function (a, b) { // replacement logic
        if (b === '[email protected]') return '';
        return a;
    }
);

As I said in my comment, the best way to achieve this would actually be with DOMParser which is a native JavaScript XML parser, but the string you have given us is not valid XML as it begins with a close tag.

share|improve this answer
add comment

This is simple enough, do tell if you need any changes:

var inputString = "</gd:organization><gd:email rel='http://schemas.google.com/g/2005#other' address='[email protected]'/><gd:email rel='http://schemas.google.com/g/2005#other' address='[email protected]' primary='true'/><gd:phoneNumber rel='http://schemas.google.com/g/2005#work'>011 360 51 60</gd:phoneNumber>";
var outputString = inputString.split("[email protected]")[1].substring(3);
alert(outputString);

I've posted a JSFiddle for it.

share|improve this answer
 
I think the email address is an unknown, and it's precisely that piece of info the OP wants to extract... –  Elias Van Ootegem Feb 2 '13 at 0:42
 
@Elias Van Ootegem - Gotcha, but I read it like my example works. 'I need to remove from the string...' and 'Based on a match to: [email protected]'. I'm not a mind reader, though ;) –  TildalWave Feb 2 '13 at 0:44
add comment

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.