Home > Software design >  Find word that does not come after word with RegEx
Find word that does not come after word with RegEx

Time:04-28

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

example: hey @hasham my email is [email protected]

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?

CodePudding user response:

Use a negative lookbehind.

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

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

DEMO

CodePudding user response:

You may use this regex with \B (reverse of word boundary):

/\B@(\w )/g

Updated RegEx Demo

  • Related