在javascript中正则表达式第n次出现时拆分字符串

在javascript中正则表达式第n次出现时拆分字符串,javascript,regex,arrays,string,split,Javascript,Regex,Arrays,String,Split,我知道split可以获得第二个参数作为限制,但这不是我想要的。我知道可以通过使用实心字符串分隔符再次拆分和连接来完成 问题是分隔符是一个正则表达式,我不知道匹配的模式的确切长度 考虑以下字符串: this is a title -------------------------- rest is body! even if there are some dashes! -------- --------------------- it should not be counted as a sep

我知道
split
可以获得第二个参数作为限制,但这不是我想要的。我知道可以通过使用实心字符串分隔符再次拆分和连接来完成

问题是分隔符是一个正则表达式,我不知道匹配的模式的确切长度

考虑以下字符串:

this is a title
--------------------------
rest is body! even if there are some dashes!
--------
---------------------
it should not be counted as a separated part!
通过使用以下命令:

str.split(/---*\n/);
我会得到:

[
  'this is a title',
  'rest is body! even if there are some dashes.!',
  '',
  'it should not be counted as a separated part!'
]
这就是我想要的:(如果我想按第一次出现的分割)

这个解决方案是我目前拥有的,但它只是第一次出现

function split(str, regex) {
    var match = str.match(regex);
    return [str.substr(0, match.index), str.substr(match.index+match[0].length)];
}
关于如何推广任意数n的解,以在正则表达式第n次出现时拆分字符串,有什么想法吗

var str= "this-----that---these------those";
var N= 2;
var regex= new RegExp( "^((?:[\\s\\S]*?---*){"+(N-1)+"}[\\s\\S]*?)---*([\\s\\S]*)$" );
var result= regex.exec(str).slice(1,3);
console.log(result);
输出:

["this-----that", "these------those"]

具有以下功能的选项:

var generateRegExp= function (N) {
    return new RegExp( "^((?:[\\s\\S]*?---*){"+(N-1)+"}[\\s\\S]*?)---*([\\s\\S]*)$" );
};

var getSlice= function(str, regexGenerator, N) {
    return regexGenerator(N).exec(str).slice(1,3);
};

var str= "this-----that---these------those";
var N= 2;
var result= getSlice(str, generateRegExp, N);
console.log(result);
var getSlice= function(str, regex, N) {
    var re= new RegExp( "^((?:[\\s\\S]*?"+regex+"){"+(N-1)+"}[\\s\\S]*?)"+regex+"([\\s\\S]*)$" );
    return re.exec(str).slice(1,3);
};

var str= "this-----that---these------those";
var N= 3;
var result= getSlice(str, "---*", N);
console.log(result);

带有功能2的选项:

var generateRegExp= function (N) {
    return new RegExp( "^((?:[\\s\\S]*?---*){"+(N-1)+"}[\\s\\S]*?)---*([\\s\\S]*)$" );
};

var getSlice= function(str, regexGenerator, N) {
    return regexGenerator(N).exec(str).slice(1,3);
};

var str= "this-----that---these------those";
var N= 2;
var result= getSlice(str, generateRegExp, N);
console.log(result);
var getSlice= function(str, regex, N) {
    var re= new RegExp( "^((?:[\\s\\S]*?"+regex+"){"+(N-1)+"}[\\s\\S]*?)"+regex+"([\\s\\S]*)$" );
    return re.exec(str).slice(1,3);
};

var str= "this-----that---these------those";
var N= 3;
var result= getSlice(str, "---*", N);
console.log(result);

谢谢。这比我的解决方案要好,但我正在寻找一种解决方案,用正则表达式的第n次出现来拆分字符串。您的解决方案仅按第一次出现拆分字符串。阿米尔,请举例说明第N次出现(N=2或更多)时的源文本和结果。想象这个字符串:“this------that------this------that”。如果N=2(基于1),结果应该是:[“这个------那个”,“这些------那些”]太好了有没有可能用正则表达式实现这一点?假设我们要编写一个函数,获取字符串、正则表达式和N,然后返回结果数组!我所说的函数是指生成所需正则表达式本身的函数,并使使用该函数的人的行为透明<代码>拆分('this------that------this------that',/---*/,2)例如。因此,我们可以通过这种方式将其与任何正则表达式一起使用!无论如何,谢谢你的回复!非常感谢:)