0

I want to get all emails in a string and then replace them wrapped in a bold tag so I can then format the email addresses to make them bold.

I've managed to mostly get this working, however I am not able to get the same email back out again in my replace.

Can someone tell me what i'm doing wrong?

What I am after is this..

First email test@test.com second email test1@test1.com

what I am getting is this...

First email test. second email test1.

var text = "First email test@test.com second email test1@test1.com";
var regexp = /[\w-\.]+@([\w-]+\.)+[\w-]{2,4}/g
var text = text.replace(regexp, '<b>$1</b>');
console.log(text);
  • The [backreference to the whole match](https://stackoverflow.com/a/53857549/3832970) is `$&` – Wiktor Stribiżew Apr 12 '21 at 12:07
  • perfect thanks for this, appreciate your answer, I did try and search to get an answer but couldn't find one – user1234567890 Apr 12 '21 at 12:10
  • I see. Most people just wrap the whole pattern with another capturing parentheses and use `$1`, but this is unnecessary tiny overhead IMHO. In your pattern, `([\w-]+\.)+` is `$1`, that is why you got the issue. – Wiktor Stribiżew Apr 12 '21 at 12:11

0 Answers0