How to replace all the special characters following another character – JavaScript?


Characters that are neither alphabetical nor numeric are known as special characters. Special characters are essentially all unreadable characters, including punctuation, accent, and symbol marks. Remove any special characters from the string to make it easier to read and understand.

We use replace() method to replace all the special characters following another character.

Using replace() method

The JavaScript built-in method string.replace() can be used to replace a portion of a supplied string with another string or a regular expression. The original string won't alter at all.

Syntax

Following is the syntax for replace() βˆ’

string.replace(searchValue, newValue)

Let’s look into the following examples for getting more idea on replacing the special character with following another character.

Example

In the following example, we are running the script to replace all the special characters following another character.

<!DOCTYPE html>
<html>
   <body>
      <script>
         let sentence1 = '<Welcome <To<TP';
         const pattern = /<(?!\s)/g;
         let result = sentence1.replace(pattern, "<@");
         document.write(result)
      </script>
   </body>
</html>

When the script gets executed, it will generate an output consisting of text that has been replaced with additional special characters on the webpage.

Example

Consider the following example, here we are replacing all the special characters with "*".

<!DOCTYPE html>
<html>
   <body style="background-color:#EAFAF1">
      <p id="tutorial"></p>
      <script>
         var statement = "The@ Be$&st^ E!-Wa%y Le@!arning%$";
         var result = statement.replace(/[^a-zA-Z ]/g, "*");
         document.getElementById("tutorial").innerHTML=result;
      </script>
   </body>
</html>

On running the above script, the output window will pop up, displaying the text that got applied with the special character "*”, which got replaced wherever the special character was found in the above code.

Example

Execute the below script and observe how all the special characters are replaced with another character.

<!DOCTYPE html>
<html>
   <body style="text-align:center;background-color:#E8DAEF;">
      <p id="tutorial"></p>
      <button onclick="applyspecial()">Click</button>
      <script>
         function applyspecial(){
            var sentence = 'MSD@ i%s #a *I^ndi*@an C$ri!!ck!eter_!-';
            var regularExpresion = /[^a-zA-Z ]/g;
            var result = sentence.replace(regularExpresion, "*!");
            document.write(result);
         }
      </script>
   </body>
</html>

When the script gets executed, it will generate an output consisting of a click button on the webpage. If the user clicks the button, the event gets triggered and displays a text that has been replaced with special characters different from the usual characters.

Updated on: 21-Apr-2023

799 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements