Click here to Skip to main content
15,917,565 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more: , +
I am working on string replacement in an contenteditable div. When user types ':', I store its index in some variable.

Consider example 1:

JavaScript
<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:

JavaScript
<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).

JavaScript
$('#editableDiv').text($('#editableDiv').text().replace(searchString, replaceString));


I even tried with
JavaScript
$('#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.
Posted
Updated 27-Dec-13 1:11am
v3
Comments
Sunasara Imdadhusen 27-Dec-13 7:16am    
Are you getting any error??
Basavaraj Metri 27-Dec-13 7:34am    
No but I have no idea how to solve this issue. I have written function to capture character index once user type ':' but have no clue how I can replace string I already have within me with the string typed after ':' till the first occurrence of white space. Thanks

1 solution

This is an interesting question, I use regex to solve it, see demo code below, note the little trick of leaving a white space between the last character of the target word and the formatting tag that follows:

XML
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("#rewrite").click(function(){
      var regex=/\.*[:]\S*/

      var originalHtml = $('#editableDiv').html();
      var newHtml = originalHtml.replace(regex,"sleeping");
      $('#editableDiv').html(newHtml);

  });
});
</script>
</head>
<body>
<form name="myform">

<div id="editableDiv"  contenteditable="true"> 
        the quick <b><i>brown</i></b> fox jumps over a <b><i>:lazy </i></b>dog
</div>

<input type="button" id="rewrite" value="Rewrite">

</form>

</body>
</html>
 
Share this answer
 
v3
Comments
Basavaraj Metri 28-Dec-13 11:05am    
Thanks Peter. I think this will fail if sentence contains more than one :lazy. It will eventually replace all available ":lazy" with "sleeping". Am I right?
Peter Leow 28-Dec-13 11:15am    
No, it will not. only the first occurrence of :. Try pasting the whole code into the "Try it yourself" here w3c js and experiment with it.
Basavaraj Metri 28-Dec-13 11:35am    
Thanks Peter, this would certainly help but I have got one more doubt, how can I replace only a particular word. Say for example I have a sentence like "the :lazy brown fox jumps over a :lazy dog". Now I would like to modify second ":lazy" only. How can I do this.
Peter Leow 28-Dec-13 14:23pm    
I think it certainly can be done. I would consider that as another question.
Basavaraj Metri 28-Dec-13 23:24pm    
Thanks a tonne Peter. I'll accept your answer and raise a new question regarding second word replacement. Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900