Search code examples
javascriptregexcodemirror

Regex: non-word-char preceded by space or at the beginning of a line


I'm trying to match @'s that are preceeded by nothing or whitespace like in the following example:

@one
    @two 
    who@three
    /@four

My approach is (^|\s)@, but this captures one two three four. \s@ only captures two. How do I get one two, without getting three four?

If it's relevant, all @'s are succeeded by a letter.


Solution

  • How about ^\s*@ ? That is, start of line followed by zero or more whitespace then @. (Note, different laguanges have different regex rules)