I am working on string replacement in an
contenteditable div. When user types ':', I store its index in some variable.
Consider example 1:
<div id="editableDiv" contenteditable="true"
the quick brown fox jumps over a :lazy dog
</div>
According to above example, the starting index is 34 and the end index must be the index of near by white space i.e., 39 and the replacing string is *sleeping*.
Given that, the ouput must be
the quick brown fox jumps over a sleeping dog
Consider example 2:
<div id="editableDiv" contenteditable="true"
the quick brown fox jumps over a lazy :dog
</div>
According to above example, the starting index is 39 and the end index must be the index of near by white space (but in this case, it must be an
EOL) and the replacing string is *cat*.
Given that, the output must be
the quick brown fox jumps over a lazy cat
I tried with below code but user would eventually lose his/her format already done to the part of the document (for ex:
brown may be bolded,
jumps may be italicised).
$('#editableDiv').text($('#editableDiv').text().replace(searchString, replaceString));
I even tried with
$('#editableDiv').html($('#editableDiv').html().replace(searchString, replaceString)); but `.html()
` will return string with html code thus index may vary accordingly.
The question remains is how to replace string without losing its format given,
starting index for search and
replace string.