Kevin Renskers
Kevin Renskers

Reputation: 5960

Negative lookbehind regex in Javascript

I want to match all strings that start with an @ unless they have other characters in front of the @ as well. Simple example: in @one @two and bla@three I want to match @one and @two but not @three. It's for highlighting usernames in a chatroom.

These strings can be anywhere in a sentence (right at the start or in the middle).

I actually thought that (?![a-zA-Z])@[a-zA-Z]+ should work but it still matches @three too.

Upvotes: 5

Views: 4837

Answers (1)

Federico Piazza
Federico Piazza

Reputation: 31035

You don't need a regex look around, you can use a simple regex like this:

\B@\w+

Working demo

enter image description here

Upvotes: 5

Related Questions