Given a string and the task is to remove a character from the given string.
Method 1: Using replace() method: The replace method is used to replace a specific character/string with other character/string. It takes two parameters, first is the string to be replaced and the second is the string which is to be replaced with. In this case, the first parameter is the character which is to be removed and the second parameter can be given as an empty string. This will remove the character from the string. This method removes the first occurrence of the string.
Syntax:
string.replace('characterToReplace', '');
Example:
<!DOCTYPE html> <html> <head> <title> How to remove a character from string using Javascript? </title> </head> <body> <h1 style="color: green"> GeeksforGeeks </h1> <b> How to remove a character from a string using Javascript? </b> <p>Original string is GeeksforGeeks</p> <p> New String is: <span class="output"></span> </p> <button onclick="removeCharacter()"> Remove Character </button> <script type="text/javascript"> function removeCharacter() { originalString = 'GeeksForGeeks'; newString = originalString.replace('G', ''); document.querySelector('.output').textContent = newString; } </script> </body> </html> |
Output:
- Before clicking the button:
- After clicking the button:
Method 2: Using replace() method with a regular expression: This method is used to remove all occurrences of the specified character, unlike the previous method. A regular expression is used instead of the string along with the global property. It will select every occurrence in the string and it can be removed.
Syntax:
string.replace(/regExp/g, '');
Example:
<!DOCTYPE html> <html> <head> <title> How to remove a character from string using Javascript? </title> </head> <body> <h1 style="color: green"> GeeksforGeeks </h1> <b> How to remove a character from a string using Javascript? </b> <p>Original string is GeeksforGeeks</p> <p> New String is: <span class="output"></span> </p> <button onclick="removeCharacter()"> Remove Character </button> <script type="text/javascript"> function removeCharacter() { originalString = 'GeeksForGeeks'; newString = originalString.replace(/G/g, ''); document.querySelector('.output').textContent = newString; } </script> </body> </html> |
Output:
- Before clicking the button:
- After clicking the button:
Method 3: Removing the first or last character using slice() method: The slice() method is used to extract parts of a string between the given parameters. This method takes the starting index and the ending index of the string and returns the string in between these indices. If the ending index is not specified, it is assumed to be the length of the string. The first character could be removed by specifying the beginning index to be 1. It extracts the string from the second character up to the end of the string. The last character could be removed by specifying the ending index to be one less than the length of the string. This extracts the string from the beginning of the string to the second to last character.
Syntax:
// Removing the first character string.splice(1); // Removing the last character string.splice(0, string.length - 1);
Example:
<!DOCTYPE html> <html> <head> <title> How to remove a character from string using Javascript? </title> </head> <body> <h1 style="color: green"> GeeksforGeeks </h1> <b> How to remove a character from a string using Javascript? </b> <p>Original string is GeeksforGeeks</p> <p> First character removed string: <span class="output1"></span> </p> <p> Last character removed string: <span class="output2"></span> </p> <button onclick="removeCharacter()"> Remove Character </button> <script type="text/javascript"> function removeCharacter() { originalString = 'GeeksForGeeks'; firstCharRemoved = originalString.slice(1); lastCharRemoved = originalString.slice(0, originalString.length - 1); document.querySelector('.output1').textContent = firstCharRemoved; document.querySelector('.output2').textContent = lastCharRemoved; } </script> </body> </html> |
Output:
- Before clicking the button:
- After clicking the button:
Method 4: Removing a particular character at given index using substr() method: This method can be used to remove a character from a particular index in the string. The substr() method is used to extract parts of a string between the given parameters. This method takes two parameters, one is the starting index and the other is the ending index of the string. The string between these indices is returned. The portion of the string before and after the character to be removed is separated and joined together. This removes the character from the specific index.
Syntax:
string.slice(0, position - 1) + string.slice(position, string.length);
Example:
<!DOCTYPE html> <html> <head> <title> How to remove a character from string using Javascript? </title> </head> <body> <h1 style="color: green"> GeeksforGeeks </h1> <b> How to remove a character from a string using Javascript? </b> <p>Original string is GeeksforGeeks</p> <p> New String is: <span class="output"></span> </p> <!-- Removing the 6th character --> <button onclick="removeCharacter(6)"> Remove Character </button> <script type="text/javascript"> function removeCharacter(position) { originalString = 'GeeksForGeeks'; newString = originalString.slice(0, position - 1) + originalString.slice(position, originalString.length); document.querySelector('.output').textContent = newString; } </script> </body> </html> |
Output:
- Before clicking the button:
- After clicking the button:
Recommended Posts:
- How to remove portion of a string after certain character in JavaScript ?
- How to remove the first character of string in PHP?
- How to remove portion of a string after a certain character in PHP?
- How to Remove Special Character from String in PHP ?
- How to get character array from string in JavaScript?
- How to get the last character of a string in JavaScript?
- Delete first character of a string in JavaScript
- How to remove all line breaks from a string using JavaScript?
- How to remove text from a string in JavaScript?
- How to remove all classes that begin with a certain string in JavaScript?
- How to remove spaces from a string using JavaScript ?
- How to remove all Non-ASCII characters from the string using JavaScript ?
- How to replace lowercase letters with an other character in JavaScript ?
- How to find out which character key is pressed using JavaScript?
- How to replace a character at a particular index in JavaScript ?
- Insert a character after every n characters in JavaScript
- JavaScript SyntaxError - Illegal character
- How to convert character to ASCII code using JavaScript ?
- Remove elements from a JavaScript Array
- How to remove an element from an array in JavaScript?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


