我一直试图干掉以下与字符串中的hashtags匹配但没有成功的正则表达式:
/^#(\w+)|\s#(\w+)/i
Run Code Online (Sandbox Code Playgroud)
这不起作用:
/^|\s#(\w+)/i
Run Code Online (Sandbox Code Playgroud)
不,我不想在开头用逗号进行交替:
/(^|\s)#(\w+)/i
Run Code Online (Sandbox Code Playgroud)
我在Ruby中这样做 - 尽管我认为这不应该是相关的.
举一些匹配和不匹配字符串的例子:
'#hashtag it is' # should match => [["hashtag"]]
'this is a #hashtag' # should match => [["hashtag"]]
'this is not a#hashtag' # should not match => []
Run Code Online (Sandbox Code Playgroud)
有什么建议?我在挑剔吗?
您可以使用.
/\B#(\w+)/i
"this is a #hash tag" # matches
"#hash tag" # matches
"this is not#hash tag" # doesn't match
Run Code Online (Sandbox Code Playgroud)