@
codehz 解决了。灵感来源:
https://github.com/zhangzhaoaaa/anyJavascript/blob/master/%E5%BC%A5%E8%A1%A5js%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F%E6%97%A0%E5%B9%B3%E8%A1%A1%E7%BB%84%E5%8A%9F%E8%83%BD.md贴上我的代码:
<script>
var str = "{'{{{{{a}+1}+1}+1}+1}'==1?\"a{\":\"b}\"} 是 {'{{{{{a}+1}+1}+1}+1}'==1?\"a\":\"b\"} {a}";
getOutStr = function(s) {
var tmpSplitTag = 'lrSplit';
var N =0;
let newStr = s;
newStr = newStr.replace(/({|})/g, function($0,$1)
{
if($1=="{") {
return "<"+tmpSplitTag+(++N)+">";
}
if($1=="}"){
return "</"+ tmpSplitTag+ (N--) +">";
}
});
let regAdd1_ = new RegExp("<"+tmpSplitTag+"1>", 'g');
newStr = newStr.replace(regAdd1_, '{');
let regAdd2_ = new RegExp("<\/"+tmpSplitTag+"1>", 'g');
newStr = newStr.replace(regAdd2_, '}');
//提取 1 的节点
let reg_ = new RegExp("{([^\}]+)}");
let responseArray = [];
function findTagStr(restStr) {
let findTag = restStr.match(reg_);
// console.log('findTag', findTag);
if(findTag) {
let findIndex = findTag.index;
if(findIndex==0) {
responseArray.push(findTag[0]);
} else {
responseArray.push(restStr.slice(0, findIndex));
responseArray.push(findTag[0]);
}
// console.log('restStr', restStr);
// console.log('findTag[0]', findTag[0]);
// console.log('slice', findIndex + (findTag[0].length));
let rightStr = restStr.slice(findIndex + (findTag[0].length));
// console.log('rightStr', rightStr);return;
if(rightStr) {
// console.log('rightStr', rightStr);return;
findTagStr(rightStr);
}
} else {
responseArray.push(restStr);
}
};
findTagStr(newStr);
//复原
let removeReg1 = new RegExp("<"+tmpSplitTag+"\\d+>", 'g');
let removeReg2 = new RegExp("<\/"+tmpSplitTag+"\\d+>", 'g');
responseArray.map(function (str_, n) {
let newStr = str_;
newStr = newStr.replace(removeReg1, '{');
newStr = newStr.replace(removeReg2, '}');
if(newStr != str_) {
responseArray[n] = newStr;
}
});
console.log('responseArray');
console.log(responseArray);
}
getOutStr(str);
</script>