开发者

Regex to parse @user mentions

开发者 https://www.devze.com 2023-04-06 15:37 出处:网络
I have been using following Regex to parse @username from posts in my application. 开发者_JAVA技巧\'/(^|\\s)#(\\w*[a-zA-Z_]+\\w*)/

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

精彩评论

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

关注公众号