In my project, I am trying to highlight a text(only usernames) whenever represented with @. I am using the following function:
function replaceMessage(data, username) {
return data.replace("@" + username, "<strong>@" + username + "</strong>");
}
This above function is working but if there are two valid users with names such as “Sam” and “Samuel” then the result after applying function is becoming something like this:
Sam and Samuel (Instead of Samuel).
How to solve this issue?
I am not that much good in using Regex, I tried the below function which is not working in IE8..
function replaceMessage(data, username) {
return data.replace(/\b + "@" + username + /g, "<strong>@" + username + "</strong>");
}
You’re going to need the special symbol
\bwhich denotes a word break in a regular expression: