5

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.

S.B.
  • 2,920
  • 1
  • 17
  • 25
Kevin Renskers
  • 5,156
  • 4
  • 47
  • 95

1 Answers1

5

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

\B@\w+

Working demo

enter image description here

Federico Piazza
  • 30,085
  • 15
  • 87
  • 123
  • As an additional example: http://regexr.com/3apif on the RegExr site if you go to reference on the left, then anchors it'll explain \b RegExr is my go to for creating RegEx as the reference guides are right there without being in the way. – zfrisch Apr 08 '15 at 21:21
  • @MicTech you could use `(?<=^| )@\w+` – Federico Piazza Mar 01 '16 at 15:54
  • @FedericoPiazza JavaScript doesn't support that. – MicTech Mar 01 '16 at 18:08
  • @MicTech true, forgot about that. Anyway, this question was posted 1 year ago and also marked as resolved, so guess it solved OP problem. – Federico Piazza Mar 01 '16 at 19:59