To remove the specific character from a JavaScript String, use the replace() method. It replaces that character with an empty character, making it look like the character is deleted. It accepts a substring or regex pattern.
const str = "Dobby"; const removed_char = str.replace("b", ""); console.log(removed_char); // Output: "Doby"
The above code has an input string called “Dobby”, and we removed the first occurrence of the character “b” by replacing it with an empty character. The final output string only contains “Doby”. One “b” is removed.
If the specific character you are trying to remove does not exist, it returns the exact input string without any changes.
const str = "Dobby"; const unchanged_str = str.replace("z", ""); console.log(unchanged_str); // Output: Dobby (No change made)
What about case-sensitivity? Well, the replace() method is case-sensitive. So, if you want to remove a lowercase letter, you need to pass a lowercase one. Otherwise, it won’t remove.
const case_str = "Eleven"; const e_removed_str = case_str.replace("e", ""); console.log(e_removed_str); // Output: Elven
In this code, we wanted to remove the first occurrence of the letter “e”.
In our input string, there is one “E” and two “e”. By definition, the replace() method should only remove the first occurrence of “e”; even if “e” occurs multiple times, it won’t replace the second “e”.
From the output, you can see that it did not replace “E” or the second “e”. Just the first, and that is what we expected.
Removing a character at a specific index
To delete a character at a specific index, apply a slice() or substring() method.
Let’s remove the character at index 4 of the input string.
const str = "instagram"; const index = 4; // Remove 'a' at index 4 // Method 1: slice() const newStr = str.slice(0, 4) + str.slice(5); console.log(newStr); // Output: instgram // Method 2: substring() const newStr2 = str.substring(0, 4) + str.substring(5); console.log(newStr2); // Output: instgram
You can see that “a” at index 4 has been removed.
Removing the last character of a string
You can use the string.slice() method to remove the last character by specifying a negative index.
let original_str = "Wednesday"; let removed_last_char = original_str.slice(0, -1); console.log(removed_last_char); // Output: Wednesda
Removing the last occurrence of a character
Use a combination of lastIndexOf() with slice() methods to eliminate the last occurrence of a character at a given position. Note that this is not the same as deleting the last character; it is the “last occurrence”.
Let’s say we have an input string called “instagram” and we want to remove the last occurrence of the character “a”. So, the output string would be “instagrm”.
const str = "instagram"; const char = "a"; const lastIndex = str.lastIndexOf(char); const last_occur = str.slice(0, lastIndex) + str.slice(lastIndex + 1); console.log(last_occur); // Output: instagrm
Removing all occurrences of a substring
You can use the replaceAll() method, which will help us replace all occurrences of a substring or pattern with an empty string, resulting in the removal of those characters.
The difference between the replace() and replaceAll() methods is that the replaceAll() method affects all matches, whereas the replace() method affects only a single match.
const str = "Addams!"; const result = str.replaceAll("d", ""); console.log(result); // Output: "Aams!"
The above output shows that we removed all the occurrences of “d” from the input string.
With Regular Expressions
We can use a regex for complex patterns with the replace() method.
For example, we can remove all the vowels from the string or eliminate all the non-alphanumeric characters from a string.
// Remove all vowels (case-insensitive) const str = "Javascript"; const replaced_regex_str = str.replace(/[aeiou]/gi, ""); console.log(replaced_regex_str); // Output: 'Jvscrpt' // Remove non-alphanumeric characters const str2 = "x1!by@3#z2$c"; const newStr2 = str2.replace(/[^a-z0-9]/gi, ""); console.log(newStr2); // Output: 'x1by3z2c'
That’s all!