1

我想标记包括哈希和现金标签在内的 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(/\b([\$#\w]+?)\b/g);
>>>> ['Bought', 'AAPL', 'today', 'because', 'of', 'the', 'new', 'iphone']

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

我可以使用哪个正则表达式来在令牌中包含前导的尖号或美元符号?

4

1 回答 1

2

明显的怎么办

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

?

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

于 2013-04-28T21:58:05.787 回答