Skip to content Skip to sidebar Skip to footer

Elements Getting Replaced At Wrong Indexes

I have a whereClause in which 'double aesteric' indicates the position where the string is to be replaced with a part of secondClause as shown in the code. I want the whereClause t

Solution 1:

The problem was when I called locations function it gave me the locations based upon the state of string before manipulation. And inside for loop the string was getting manipulated, so the original locations were not valid anymore. Had to call locations function at each iteration of the loop to get the latest location. That fixed my problem.

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 ++;
}

Post a Comment for "Elements Getting Replaced At Wrong Indexes"