Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need a functionality of find and replace.I could replace and find the text. But couldn't to highlight the text in textarea and also how to get the occurrences of found texts.If it is first occurrence,the highlighted color should be different from rest of others.As such I need in my project. But am blank how to do this.Can you please reply with the code how to achieve it.
Posted

1 solution

The replace() method searches a string for a specified value, or a regular expression, and returns a new string where the specified values are replaced.
<body>

<p>Click the button to replace "blue" with "red" in the paragraph below:</p>

<p id="demo">Mr Blue has a blue house and a blue car.</p>

<button onclick="myFunction()">Try it</button>

<script>
function myFunction() {
    var str = document.getElementById("demo").innerHTML; 
    var res = str.replace(/blue/g, "red");
    document.getElementById("demo").innerHTML = res;
}
</script>

</body>
 
Share this answer
 
Comments
Vidhya Raju 6-Sep-14 4:29am    
How to highlight all the occurrences of replaced texts in textarea

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