0

I've looked at a lot of stack posts and Google results but I can't quite find my answer... I have this snippet of text.

<p>Lorem [email protected] , consectetur adipisicing elit. Eos, doloribus, dolorem iusto blanditiis unde eius illum consequuntur neque dicta incidunt ullam ea hic porro optio ratione repellat</p>

I'm trying to target the email addresses and then underline them. It's important that the body is selected rather than the <p> tag. I've made an expression that I know works since I made it via RegexPlanet.

So far, I have this:

var result = $("body").text();
var newResult =result.replace("\b[\w\.-]+@[\w\.-]+\.\w{2,4}\b", "REPLACED");
console.log(newResult);

But it doesn't seem to work... If you go to this Here and then click JavaScript, you will see the result of input.replace(). That is essentially what I'm after but I'd also like to be able to use jQuery's .css() function.

How can I do this?

2
  • how does you need to use .css come into play? Commented Sep 27, 2015 at 1:22
  • 2
    When you read the text and change it, it does not automatically map back to the element. You need to set the HTML back. And by using text() you will strip all HTML from the page. Commented Sep 27, 2015 at 1:37

2 Answers 2

1

I could make it work by using html() instead of text(), changing the regex and reassigning the result to body:

var result = $("body").html();
var newResult = result.replace(/\b[\w\.-]+@[\w\.-]+\.\w{2,4}\b/g, "REPLACED");
$("body").html(newResult);

Instead of using "REPLACED" in the replace function you could use a function to surround the email within a span like this:

var newResult = result.replace(/\b[\w\.-]+@[\w\.-]+\.\w{2,4}\b/g,
    function (x) { return '<span class="underlined">' + x  + '</span>';});
Sign up to request clarification or add additional context in comments.

2 Comments

How could I edit my expression to do what it does right now but also make sure the domain of the email is always @xyz.com for instance? If the email address didn't contain @xyz.com then it would simply ignore it?
/\b[\w\.-][email protected]\b/g
0

Try this, without quotes on the regex:

var result = $("body").html();
var newResult = result.replace(/\b[\w\.-]+@[\w\.-]+\.\w{2,4}\b/ig, "REPLACED");
console.log(newResult);
//Lorem REPLACED , consectetur adipisicing elit.....

Notes:

Use $("body").html(); instead of $("body").text(); in order to keep the html tags.
Your regex is weak to match emails, try to improve it.


Demo:

http://codepen.io/anon/pen/epBvxG

1 Comment

How could I edit my expression to do what it does right now but also make sure the domain of the email is always @xyz.com for instance? If the email address didn't contain @xyz.com then it would simply ignore it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.