I have the following regex:
/#([A-Za-z0-9_]+)/g
How can I make it so that there must be a space before the hashtag for the regex to be valid (i.e. the second line in the link above shouldn't match).
I have the following regex:
/#([A-Za-z0-9_]+)/g
How can I make it so that there must be a space before the hashtag for the regex to be valid (i.e. the second line in the link above shouldn't match).
From:
/#([A-Za-z0-9_]+)/
To:
/\s#([A-Za-z0-9_]+)/
More information on matching space/s: Matching a space in regex
You can simply add a space in your regex as follows:
/ #([A-Za-z0-9_]+)/g
To not include the space in the match, you can do:
/(?:\s)(#[A-Za-z0-9_]+)/g
Use \K or look behind to not to include space at the end result.
/ \K#([A-Za-z0-9_]+)/g
OR
(?<=\s)#([A-Za-z0-9_]+)
First captured group contains the string test
. If you want the captured group to contain #
, the use this regex (?<=\s)(#[A-Za-z0-9_]+)