I am trying to detect whether a block of text (from a textarea) contains words that are prefixed with the @sign.
For example in the following text: Hey @John, i just saw @Smith
It will detect John and Smith respectively without the @ symbol. I reckoned something like this would work:
@\w\w+
My question is how do i make javascript filter the text, assuming it is stored in a variable comment?
It should output only the names in the text that are prefixed with @ without the @ symbol.
Regards.
You use the
g(global) flag, a capture group, and a loop callingRegExp#exec, like this:Output:
Live example | source
With thanks to @Alex K for the boundary recommendation!