Javascript 将包含非单词字符的单词的字符串标记化

Javascript 将包含非单词字符的单词的字符串标记化,javascript,regex,tokenize,Javascript,Regex,Tokenize,我想标记Twitter消息,包括散列和现金标签。标记化的正确示例如下: "Bought $AAPL today,because of the new #iphone".match(...); >>>> ['Bought', '$AAPL', 'today', 'because', 'of', 'the', 'new', '#iphone'] 我为此任务尝试了几个正则表达式,即: "Bought $AAPL today,because of the new #iphone

我想标记Twitter消息,包括散列和现金标签。标记化的正确示例如下:

"Bought $AAPL today,because of the new #iphone".match(...);
>>>> ['Bought', '$AAPL', 'today', 'because', 'of', 'the', 'new', '#iphone']
我为此任务尝试了几个正则表达式,即:

"Bought $AAPL today,because of the new #iphone".match(/\b([\w]+?)\b/g);
>>>> ['Bought', 'AAPL', 'today', 'because', 'of', 'the', 'new', 'iphone']

我可以使用哪种正则表达式,在代币中包含最前面的夏普或美元符号?

如何

"Bought $AAPL today,because of the new #iphone".match(/[$#]*\w+/g)
// ["Bought", "$AAPL", "today", "because", "of", "the", "new", "#iphone"]
?


注:
[$\]*
可能会被
[$\\]]替换?
,不确定确切的要求。

@Ejay:有时人们使用
也很抱歉在阅读你的评论之前删除了我的评论:)
“今天买了$AAPL,因为新的iphone”。match(/[^,\!\?\。]+/g)
你能发布可能字符串的示例吗?编辑:你的答案是:)这是可行的,我认为使用单词分隔符(
\b
)会更省钱,但我现在想不出一个适用于这种情况的边缘案例,所以你的解决方案应该可以完美地工作,谢谢。
"Bought $AAPL today,because of the new #iphone".match(/[\b^#\$]([\w]+?)\b/g);
>>>> ['$AAPL', '#iphone']
"Bought $AAPL today,because of the new #iphone".match(/[$#]*\w+/g)
// ["Bought", "$AAPL", "today", "because", "of", "the", "new", "#iphone"]