I’ve looked through numerous other questions up here surrounding a negative lookbehind for JavaScript but I can’t seem to mimic them. I have the following Regex:
\b((?:http[s])?\S+\.\S+\.(?:\S+\.?){1,4})(?<!@)\b`
and the basis of this Regex is it matches web addresses like:
www.msn.com- or
http://www.msn.com - or
https://www.google.com - or
http://msdn.microsoft.com.
However, it also matches name@d1.d2.d3.d4 because of the numerous domain names following the @ sign. I need to perform a negative lookbehind for that @ sign.
Hopefully somebody can give me a hand!
UPDATE
Updating to include the replacement string that I’m using to create the hyperlink out of the matched text.
replace(webLinkPattern, "<a href=\"http://$2\" target=\"_blank\" onclick=\"preventDualEditing(event)\">$2</a>");
UPDATE 2 – ANSWER
ts = ts.replace(webLinkPattern, function (url) {
if (url.indexOf('@') != -1) {
return url;
}
return url.replace(webLinkPattern, "<a href=\"http://$&\" target=\"_blank\" onclick=\"preventDualEditing(event)\">$&</a>");
});
JavaScript doesn’t support lookbehind assertions, so you’ll have to handle this in your own code. One option is simply to include the
@in the result, and then discard the match if the@is there:Alternatively, you can check for a preceding
@in the callback, even without including it inurl:(Note that I’ve also changed
http[s]tohttps?, which is what you meant.http[s]is the same ashttps, since[s]is a character class matching any character that’ss.)