I have this regex that detects hashtags. It shouldn’t match things with letters before them, so we’ve got a space character at the beginning of the regex:
/( #[a-zA-Z_]+)/gm
The issue is it no longer matches words at the beginning of sentences. How can I modify this regex so that instead of matching with spaces, it simply DOESN’T match things with letters before them.
Thanks!
Use\bat the start to indicate a word boundary.\bwon’t work, since#isn’t a word starter.Just check for the start of the string or a space before:
(?:^|\s)(\#[a-zA-Z_]+)Also, make sure you escape the
#, so it doesn’t get interpreted as a comment.