gpt4 book ai didi

Javascript - string.split(regex) 保留分隔符

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:10:02 25 4
gpt4 key购买 nike

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

在java中我使用:

theString.split("(?<=[!><=}{])|(?=[!><=}{])|(?<= AND )|(?= AND )|(?<= OR )|(?= OR )")

但是,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"

拆分:

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]+" |");
}

最佳答案

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

thestr.split(/([!><=}{]| (?:AND|OR) )/)

这会返回一个数组,如下所示:

["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"]

然后你只需要过滤空字符串就大功告成了:

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

编辑 由于 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];
}

这基本上是将分隔符与其他部分分开,然后将两者结合起来。

关于Javascript - string.split(regex) 保留分隔符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4204210/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com