问题是,当我调用locations函数时,它根据字符串在操作之前的状态给出了位置。在for循环中,字符串被操纵,因此原始位置不再有效。必须在循环的每次迭代中调用locations函数以获取最新位置。那解决了我的问题。
var whereClause = '(**kon = "link"**) AND (**kon = "link"**) AND (king = "long")';
var secondClause = '(cop != "kong") AND (king = "king")';
secondClause = secondClause.split(') AND (');
var counter = locations('**',whereClause);
var secondIndex = 0;
var new_whereClause = whereClause;
var secondIndex = 0;
for (let index = 0; index <= (arr.length / 2); index += 2) {
var arr = locations('**',new_whereClause);
//console.log('arr :',arr,secondClause);
var chucnkStr = new_whereClause.substring(arr[0],arr[1]+2); // adding 2 because substring returns location of second instance one less than actual.
new_whereClause = new_whereClause.replace(chucnkStr,secondClause[secondIndex].replace(/[()]/g, ''));
secondIndex ++;
}