Javascript-string.split(regex)保留分隔符 [英] Javascript - string.split(regex) keep separators
问题描述
我想使用正则表达式拆分字符串,并在结果数组中包含分隔符/匹配的信息.
I would like to split a string using a regular expression and have the separators / matched info included in the resulting array.
在Java中,我使用了:
In java I used:
theString.split("(?<=[!><=}{])|(?=[!><=}{])|(?<= AND )|(?= AND )|(?<= OR )|(?= OR )")
但是,javascript不支持向后隐藏?< =
But, javascript doesn't support 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"
要拆分:
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
我所拥有的例子:
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]+" |");
}
推荐答案
如果将整个模式放在一个组中,您还将获得分隔符:
If you put the whole pattern in a group, you will also get the separators:
thestr.split(/([!><=}{]| (?:AND|OR) )/)
这将返回一个数组,如:
This returns an array like:
["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"]
然后,您只需要过滤空字符串就可以了:
Then you just need to filter the empty strings and you’re done:
thestr.split(/([!><=}{]| (?:AND|OR) )/).filter(Boolean)
编辑由于Internet Explorer和可能的其他浏览器未将分组分隔符带入结果数组,因此您可以这样做:
Edit Since Internet Explorer and possibly other browsers do not take a grouped separator into the result array, you could do this instead:
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];
}
这基本上将分隔符与其他部分分开,然后将两者组合.
This basically separates the separators from the other parts and then combines both.
这篇关于Javascript-string.split(regex)保留分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!