1

I'm trying to parse a DOM and replace parts of the text with tokens, for example detect an email then replace it with {{EMAIL}} string without changing the HTML Given the following HTML (for example, can be different):

<div dir="ltr" style="direction: ltr; text-align: left;">
   this is <span style="font-style:italic;">my</span>
    email <span title="bla@bla.com" style="font-weight:bold;">bla@bla.com&nbsp;</span>
</div>

How can I replace just the test portion so it to would be:

<div dir="ltr" style="direction: ltr; text-align: left;">
   this is <span style="font-style:italic;">my</span>
    email <span title="bla@bla.com" style="font-weight:bold;">{{EMAIL}}&nbsp;</span>
</div>
Shlomi Schwartz
  • 8,693
  • 29
  • 109
  • 186

3 Answers3

1

you could add an id="myspan" to the DOM element that you need to change and with the help of jquery change the innerHTML , like so:

HTML:

<div dir="ltr" style="direction: ltr; text-align: left;">
   this is <span style="font-style:italic;">my</span>
    email <span id="myspan" style="font-weight:bold;">{{EMAIL}}&nbsp;</span>
</div>

JS:

$('#myspan').innerHTML = "{{EMAIL}}&nbsp;";
Theo Itzaris
  • 4,321
  • 3
  • 37
  • 68
1

You need to select element has title attribute and filter selected element using .filter() that select element has email address in title attribute. Then replace email address with new text.

$("div > [title]").filter(function(){
    return $(this).attr("title").match(/^[\w]+@[\w.]+\.[\w]+$/g);
}).text(function(i, text){
    return text.replace(/^[\w]+@[\w.]+\.[\w]+/g, "{{EMAIL}}");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div dir="ltr" style="direction: ltr; text-align: left;">
   this is <span style="font-style:italic;">my</span>
    email <span title="bla@bla.com" style="font-weight:bold;">bla@bla.com&nbsp;</span>
</div>
Mohammad
  • 21,175
  • 15
  • 55
  • 84
  • Thanks for the reply, title is just an example. I have no control over the input HTML – Shlomi Schwartz Oct 10 '16 at 15:43
  • @ShlomiSchwartz You said *if an email string exists in a title attribute for example, it will be replaced*. The code work for any tag. – Mohammad Oct 10 '16 at 15:46
  • Regex would be the way to go if you need to find all instances of an email address written as text (although you *should* be annotating these tags with data attributes to indicate they contain the text that needs to be changed). Alternatively, I believe you could convert the entire DOM to a string, run regex on the entire string and use match groups to return all nodes containing email address(es) in their content. See the "CheatSheet" link at [regexr.com](http://regexr.com/) for more information on match groups and regex. – Ross Brasseaux Oct 10 '16 at 15:54
  • Regex could be a solution, but if the entire Dom is a string then email would be replaced in attributes to, when what I want is to replace just the text of a tag – Shlomi Schwartz Oct 10 '16 at 16:08
  • @ShlomiSchwartz `.text()` only return text of element and `text.replace()` only replace email in text. – Mohammad Oct 10 '16 at 16:11
  • @Mohammad what is the element looks like that a@a.com
    some text
    . Wouldn't the
    be removed?
    – Shlomi Schwartz Oct 10 '16 at 16:17
  • @ShlomiSchwartz That isn't a good think that you changed your question or your html after answering. Current code remove `br` from html but you can use `.html()` instead `.text()` that keep it. – Mohammad Oct 10 '16 at 16:25
  • @Mohammed, sorry for that. html() won't do, since it will return children that could potentially match the pattern. – Shlomi Schwartz Oct 10 '16 at 20:22
1

If you need to find and replace texts, you can always use JQuery's :contains selector. Like the example found here: Find text string using jQuery?

You can get all occurrences of your desired string by using $('span:contains("bla@bla.com") or even *:contains if you are not searching for a specific tag.

After you got all your instances, you can do a text replace, as suggested by Mohammad

Community
  • 1
  • 1
Ant
  • 1,083
  • 1
  • 15
  • 35
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/15765498) – Julian Apr 06 '17 at 20:05
  • Obvious issue I didn't have thought. Thanks for pointing it out! – Ant Apr 06 '17 at 20:22