【发布时间】:2013-04-28 21:52:21
【问题描述】:
我想标记 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']
我可以使用哪个正则表达式来在标记中包含前导的尖号或美元符号?
【问题讨论】:
-
@Ejay:有时人们也会使用
.、!、? -
抱歉在阅读你之前删除了我的评论 :)
"Bought $AAPL today,because of the new #iphone".match(/[^ ,\!\?\.]+/g) -
实际上可以请您发布可能字符串的示例吗?编辑:你有答案:)
标签: javascript regex tokenize