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

Find word that does not come after word with RegEx

I’m trying to get all the text which start with @ and replace them with the needed text

here’s it’s regex

/@([a-z_\d]+)/gi

Now the issue I’m facing is I just wanted to get the text if it’s alone like

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

example: hey @hasham my email is vakanihasham@gmail.com

When I run the regex, it should provide me with the @hasham value but not the @gmail. I tried a solution by addingin \s to check empty space before the matching parameter but the issue I’m having is that when the @hasham is first in the paragraph, it doesn’t get it because there is no empty space before trying to find a way. How can I simply select the independent words that fit my regex?

>Solution :

Use a negative lookbehind.

/(?<!\S)@(\w+)/g

This matches @ if it’s not preceded by a non-whitespace character.

DEMO

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