I’ve looked at other posts here on Stack Overflow with the same issue, but I’m still unable to solve my exact issue.
For this project, users are able to tag other users (like Facebook, etc.). However, you tag someone by surrounding their name with the @ symbol; eg: @username@
Site works perfectly fine, however, the site completely refuses to load on any Safari browser (mobile and on my Mac). I’ve tracked it down to Safari not supporting lookbehind regex expressions, but I’m already a regex-noob to begin with, and am having difficulties getting around this. Usernames can be of varying length, and can have spaces and special characters.
From the information I’ve gathered, there is no other regex alternative to lookbehinds.
My Regex that works outside of Safari:
text.replace(/(?<=\@)(.*?)(?=\@)/g, '<span style="color: #9f56af; font-weight: 500; cursor: pointer;" data-tag>$1</span>');
>Solution :
A solution is to match @ and add it in the replacement string:
let text = "Hey @username@ !"
text = text.replace(/@(.*?)@/g, '@<span style="color: #9f56af; font-weight: 500; cursor: pointer;" data-tag>$1</span>@');
console.log(text)