Javascript - string.split(正则表达式)保留分隔符

Caz*_*azz 3 javascript regex

我想使用正则表达式拆分字符串,并在结果数组中包含分隔符/匹配信息.

在java中我用过:

theString.split("(?<=[!><=}{])|(?=[!><=}{])|(?<= AND )|(?= AND )|(?<= OR )|(?= OR )")
Run Code Online (Sandbox Code Playgroud)

但是,javascript不支持lookbehind ?<=

例如,我想要字符串:

"Reason={Existing problem or fault}{Bestaande probleem of vout}{Other}{Ander} and Required!=No and Results >=10 and Results <=25 and Tst>5 and Tst<80 and Info=test this or that and those and Success!=Yes"
Run Code Online (Sandbox Code Playgroud)

分开:

Reason,=,{,Existing problem, or ,fault,},{,Bestaande probleem of vout,},{,Other,},{,Ander,}, and ,Required,!,=,No, and ,Results,>,=,10, and ,Results,<,=,25, and ,Tst,>,5, and ,Tst,<,80, and ,Info,=,test this, or ,that, and ,those, and ,Success,!,=,Yes
Run Code Online (Sandbox Code Playgroud)

我得到的例子:

var thestr = "Reason={Existing problem or fault}{Bestaande probleem of vout}{Other}{Ander} and Required!=No and Results >=10 and Results <=25 and Tst>5 and Tst<80 and Info=test this or that and those and Success!=Yes";

document.write("::SPLIT::<br>");
var patt1=new RegExp(/([!><=}{])|( AND )|( OR ) /gi);

var x = thestr.split(patt1);
//This splits correctly but, doesn't include the separators / matched characters
document.write("length="+x.length+"<br>");
for (c=0;c<x.length;c++) {
    document.write(c+" - "+ x[c]+" |");
}

document.write("<br><br>::MATCH::<br>");

var y = thestr.match(patt1);

//This shows the matched characters but, how do I combine info from split and match
document.write("length="+y.length+"<br>");
for (d=0;d<y.length;d++) {
    document.write(d+" - "+ y[d]+" |");
}

document.write("<br><br>::INCLUDE SEPERATORS::<br>");
var patt2=new RegExp(/(?![!><=}{])|(?=[!><=}{])|(?! AND )|(?= AND )|(?! OR )|(?= OR ) /gi);
//This puts everything in the array, but, each character is a seperate array element.
// Not what I wanted to achieve.
var bits = thestr.split(patt2);
document.write("length="+bits.length+"<br>");
for (r=0;r<bits.length;r++) {
    document.write(r+" - "+ bits[r]+" |");
}
Run Code Online (Sandbox Code Playgroud)

Gum*_*mbo 5

如果将整个模式放在一个组中,您还将获得分隔符:

thestr.split(/([!><=}{]| (?:AND|OR) )/)
Run Code Online (Sandbox Code Playgroud)

这将返回一个数组,如:

["Reason", "=", "", "{", "Existing problem or fault", "}", "", "{", "Bestaande probleem of vout", "}", "", "{", "Other", "}", "", "{", "Ander", "}", " and Required", "!", "", "=", "No and Results ", ">", "", "=", "10 and Results ", "<", "", "=", "25 and Tst", ">", "5 and Tst", "<", "80 and Info", "=", "test this or that and those and Success", "!", "", "=", "Yes"]
Run Code Online (Sandbox Code Playgroud)

然后你只需要过滤空字符串就可以了:

thestr.split(/([!><=}{]| (?:AND|OR) )/).filter(Boolean)
Run Code Online (Sandbox Code Playgroud)

编辑     由于Internet Explorer和可能的其他浏览器不将分组分隔符放入结果数组中,您可以这样做:

var matches = thestr.split(/(?:[!><=}{]| (?:AND|OR) )/),
    separators = thestr.match(/(?:[!><=}{]| (?:AND|OR) )/g);
for (var i=0; i<separators.length; ++i) {
    matches[i+1] = separators[i];
}
Run Code Online (Sandbox Code Playgroud)

这基本上将分离器与其他部分分开,然后将两者结合起来.