如果我有一个类似的字符串:
this is a #tag in the middle.
我该如何表达正则表达式以将其拆分为:
["this is a", "#tag", "in the middle."]
从本质上讲,我正在寻找一个以#
开头的块,直到该行的末尾或下一个空格。
答案 0 :(得分:0)
捕获split
内的组将包含在结果中,因此您可以使用模式\s*(#\S+)\s*
:
const str = 'this is a #tag in the middle.';
console.log(
str.split(/\s*(#\S+)\s*/)
)