示例输入:- (;1A;<1&&;2;<2&&;100Baxc02;<3&&;1000D33;<4&&;10001HGA;<5)
预期输出:- 1A,2,100Baxc02,1000D33,10001HGA
注意:- 输入的长度不固定,它会变化。
到目前为止我的代码:
var AlertExpressionWithUid = "(;1A;<1&&;2;<2&&;100Baxc02;<3&&;1000D33;<4&&;10001HGA;<5)";
theArray = AlertExpressionWithUid.split(';');
output="";
for (i = 0; i < theArray.length; i++) {
theelm = theArray[i];
output = output + theelm;
}
alert(output);
最佳答案
看起来您想要匹配 ;
之间不包含 &&
的子字符串 - 为什么不使用正则表达式?使用后视:
const AlertExpressionWithUid = "(;1A;<1&&;2;<2&&;100Baxc02;<3&&;1000D33;<4&&;10001HGA;<5)";
const matches = AlertExpressionWithUid.match(/(?<=;)[^;&]+(?=;)/g);
console.log(matches);
或者,由于仅在较新的浏览器中支持lookbehind,如果没有lookbehind,您将不得不使用循环:
const AlertExpressionWithUid = "(;1A;<1&&;2;<2&&;100Baxc02;<3&&;1000D33;<4&&;10001HGA;<5)";
const re = /;([^;&]+);/g;
let match;
const matches = [];
while ((match = re.exec(AlertExpressionWithUid)) !== null) {
matches.push(match[1]);
}
console.log(matches);
关于javascript - 使用java脚本或jquery从字符串中如何获取两个分隔符之间提到的值列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51299381/