Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我一直在使用以下正则表达式从我的应用程序中的帖子中解析@username。
'/(^|\s)#(\w*[a-zA-Z_]+\w*)/
有人可以解释我的目的(^|\s)。如果我省略那部分怎么办?
(^|\s)
(^|\s)匹配字符串的开头 ( ^) 或空格字符 ( \s)。这是为了防止hallo#world匹配作为提及。
^
\s
hallo#world
另一种方法是使用\b(单词边界)。它的语义略有不同,但在这种情况下应该可以工作。
\b
(^|\s)是行首或字符串 ( ^) 或 ( |) 空格字符 ( \s)
|