I have been using following Regex to parse #username from posts in my application.
'/(^|\s)#(\w*[a-zA-Z_]+\w*)/
Can somebody explain me the purpose of (^|\s)
. What if I omit that part?
(^|\s)
either matches the beginning of a string (^
) or a space character (\s
). This is in order to prevent hallo#world
from matching as a mention.
An alternative to that is using \b
(a word boundary). It has slightly different semantics, but it should work in this case.