How to replace a text at a the same index. This text may have the same pattern. Say I want to replace the bold "is" in the next text.
"Sam is a boy and Dory is his sister, but John is his father. He isn't a doctor."
when I use the following code it replaces the first pattern which is the italicized "is"!
function getSelectedHTMLText() {
if (window.getSelection) { // all browsers, except IE before version 9 contents
var range = window.getSelection ();
var container = document.createElement("div");
for (var i = 0, len = range.rangeCount; i < len; ++i) {
container.appendChild(range.getRangeAt(i).cloneContents());
}
html = container.innerHTML;
return html;
}
else {
if (document.selection.createRange) { // Internet Explorer
var range = document.selection.createRange();
html = range.htmlText;
return html;
}
}
}
var txt = "Sam is a boy and Dory is his sister, but John <b>is</b> his father. He isn't a doctor.";
var htmlText = getSelectedHTMLText(); //This return s
var txtModified = txt.replace(htmlText, "is");
Note: I use this code to replace an HTML code with none-HTML code and vice versa