Skip to content

Remove specific character from string JavaScript | Code

  • by

Use replace() method to remove specific character from string JavaScript. This method searches a string for a value or a regular expression and returns a new string with the value(s) replaced.

string.replace(searchValue, newValue)

The repace() method takes two parameters, the first is the String to be replaced, and the second is the String, which is to be replaced with. In this case, the first argument is the character to be removed, and the second parameter can be given as the empty string.

Remove specific character from string JavaScript

Simple example code removes the first occurrence of the String.

<!DOCTYPE html>
<html lang="en">
<body>
  <script>
    
    str = 'Hello cy Adele';

    newStr = str.replace('c', '');

    console.log('Original String: ', str);
    console.log('After character removed: ', newStr);

  </script>
</body>
</html>

Output:

Remove specific character from string JavaScript

Use replaceAll() to replace all occurrence of givne char

<script>

    var oldString = "Hello World!";
    var newString = oldString.replaceAll("o", "");
    console.log(newString); // Hell Wrld!

</script>

Using a replace() method with a regular expression to select every occurrence in a string, and remove it.

  <script>

    str = 'AppDividend';

    newStr = str.replace(/d/g, '');
    console.log(newStr);// AppDivien

  </script>

Do comment if you have any doubts or suggestions on this Js remove char code.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

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