Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
  
</body>
</html>
 
function escape(s) {//escapes a regex, from 3561493
    return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
}
var array = [
"<b>Test</b>",
"of the <div style=\"color:red\">battle</div> system"
];
var cElem = document.createElement("span");
var cleanArray = array.map(function(elem){
   cElem.innerHTML =  elem;
   return cElem.textContent;
});
var s = "Test"
for(var i=0;i<array.length;i++){
  var idx;
  while((idx = s.indexOf(cleanArray[i],idx)) > -1){
     s = s.replace(cleanArray[i],array[i]);
     idx +=(array[i].length - cleanArray[i].length) +1;//update the index
  }
}
alert(s);
document.write(s);
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers