开发者

Regex to parse @user mentions

I have been using following Regex to parse @username from posts in my application.

开发者_JAVA技巧
'/(^|\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.


(^|\s) is either the start of the line or string (^) or (|) a white space character (\s)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜