replacing words in paragraph using javascript

Sometimes it is important to replace some words from a paragraph. This can be done at client end using javascript. In the following example, we want to replace two words “class” and “javascript” from the text written in textarea. The words are replaced with “***” strings.

<!DOCTYPE html>
    <title>Untitled document</title>
  <meta charset="utf-8" />

    <script language="javascript" type="text/javascript">
        function myfunction()
            var txt = document.getElementById("txta");

            txt.value = txt.value.replace(/class/gi, '***');
            txt.value = txt.value.replace(/javascript/gi, '***');
<textarea id="txta">This is web engineering class and we are learning javascript class.</textarea>
    <br />
<input id="btn" name="btn" type="button" 
   value="Click"    onclick="myfunction();" />



Add a Comment

Your email address will not be published. Required fields are marked *