我想将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']
我可以使用哪种正则表达式,在令牌中包含前导锐利或美元符号?
答案 0 :(得分:2)
显而易见的
"Bought $AAPL today,because of the new #iphone".match(/[$#]*\w+/g)
// ["Bought", "$AAPL", "today", "because", "of", "the", "new", "#iphone"]
PS:[$#]*
可能会被[$#]?
取代,不确定具体要求。