正则表达式问题-组的多个匹配

时间:2018-11-14 12:33:09

标签: javascript regex

我有不同的文字。匹配的顺序和发生的方式不同:

This is a text: <<<String(10)>>>\nToday is the: <<<Date(10)>>>

<<<String(10)>>> This was a text: \nToday is the: <<<Date(10)>>>

<<<String(10)>>><<<Date(10)>>> This was a text and a date...

现在,我搜索一个正则表达式,该正则表达式为我提供了一系列匹配项。第一个例子应该给我:

['This is a text: ', '<<<String(10)>>>', '\nToday is the: ', '<<<Date(10)>>>']

我尝试过这样的事情:

/(.*?)(\<{3}.*?\>{3})/

这适用于第一个示例的顺序,但不适用于不同的顺序。请给我一个提示!

1 个答案:

答案 0 :(得分:0)

使用

s.split(/(<{3}.*?>{3})/)

会做的工作,但会给一个空的条目。如果要删除它,可以使用:

s.split(/(<{3}.*?>{3})/).filter(str => str.length > 0)

var s = "This is a text: <<<String(10)>>>\nToday is the: <<<Date(10)>>>";
var arr = s.split(/(<{3}.*?>{3})/).filter(str => str.length > 0);
console.log(arr)