Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regex Lookbehind not supported in Safari

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading