This question already has an answer here:
这个问题在这里已有答案:
- Split string by hashtag and save into array with jQuery? 5 answers
通过hashtag拆分字符串并使用jQuery保存到数组中? 5个答案
For example: Lorem #ipsum dolor sit #amet
例如:Lorem #ipsum dolor sit #amet
Using a regex how do i take #ipsum and #amet and put them into an array?
使用正则表达式如何使用#ipsum和#amet并将它们放入数组中?
1 个解决方案
#1
2
You can use:
您可以使用:
/#\w+/g
To get each word with a hashtag. Note you can use *
instead of +
if you also want to match the hashtags themselves. Then you can use .match()
, which will return an array of the matched items. For example:
用标签获取每个单词。请注意,如果您还想匹配主题标签,则可以使用*而不是+。然后你可以使用.match(),它将返回匹配项的数组。例如:
var reg = /#\w+/g;
console.log("Lorem #ipsum dolor sit #amet".match(reg));
Note: \w
will match word characters ([a-zA-Z0-9_]
), if you want any text other than whitespace use \S
instead.
注意:\ w将匹配单词字符([a-zA-Z0-9_]),如果你想要除空白之外的任何文本使用\ S代替。
#1
2
You can use:
您可以使用:
/#\w+/g
To get each word with a hashtag. Note you can use *
instead of +
if you also want to match the hashtags themselves. Then you can use .match()
, which will return an array of the matched items. For example:
用标签获取每个单词。请注意,如果您还想匹配主题标签,则可以使用*而不是+。然后你可以使用.match(),它将返回匹配项的数组。例如:
var reg = /#\w+/g;
console.log("Lorem #ipsum dolor sit #amet".match(reg));
Note: \w
will match word characters ([a-zA-Z0-9_]
), if you want any text other than whitespace use \S
instead.
注意:\ w将匹配单词字符([a-zA-Z0-9_]),如果你想要除空白之外的任何文本使用\ S代替。