0

我怎么能做到这样的事情?

example_input = "hi, this is example #input, it has a few #different things going #on. #question"

output ===> [input, different, on, question]

这是我到目前为止所拥有的:

text.match(/\#.+?\s+/g)

当后面跟着空白时,我已经能够进入隔离状态,但我不确定如何适应, ' ' .string end / new line

4

2 回答 2

1
/#\w+\b/g

\b 用于边界字符

对于空格、逗号和字符串结尾,请使用 '\s'、',' 和 '$'

/#\w+(\s?|.|$)/g   

这是您可以使用的特殊字符列表(在特殊字符下阅读):

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/RegExp

于 2012-10-24T18:41:51.913 回答
0

这将从 # 开始匹配,直到找到空格或标点符号。

/#[^\s\p{P}]+/
于 2012-10-24T18:46:38.367 回答