Remove specific characters from a string in JavaScript
This post will discuss how to remove specific characters from a string in JavaScript.
To remove specific characters from a string in JavaScript, we need to use some function that can find and delete the characters from the string, and return a new string without them. Here are some of the methods that we can use, along with some examples:
1. Using replace() function
We can use the replace() built-in function with a regular expression that matches the characters we want to remove, and replace them with an empty string. This is a built-in function of the String object that takes two parameters, the first is the string or regular expression to be replaced and the second is the string that is to replace it. To remove characters, we can use an empty string as the second parameter. For example, if we want to remove all vowels (a, e, i, o, u) from the string "Hello World!", we can write:
|
1 2 3 4 5 6 |
let str = "Hello World!"; let result = str.replace(/[aeiou]/g, ""); // result is "Hll Wrld!" console.log(result); |
The regular expression /[aeiou]/g matches any character that is in the brackets (a, e, i, o, u) globally (g), meaning it will find all occurrences of the specified characters in the string. The replacement value is an empty string (""), which will replace each matched character with nothing.
2. Using split() and join() functions
We can use the split() and join() built-in functions of the String object to split the string by the characters we want to remove, and then join the resulting array with an empty string. For example, if we want to remove all spaces (" ") from the string "Hello World!", we can write:
|
1 2 3 4 5 6 |
let str = "Hello World!"; let result = str.split(" ").join(""); // result is "HelloWorld!" console.log(result); |
The split() function will return an array of substrings that are separated by the space character (" "), such as ["Hello", "World!"]. The join() function will return a string that is composed of the array elements joined by an empty string ("").
3. Using slice() function
The slice() is another built-in function of the String object that takes two parameters, the start index and the end index of the substring to be extracted. This function returns a new string that contains the characters between the start and end indices, not including the end index. To remove characters, we can use negative indices or omit the end index. This function can only remove characters from the beginning or the end of the string, not from the middle. For example:
|
1 2 3 4 5 6 7 8 9 |
let str = "Hello World!"; // returns: "World!" let result = str.slice(6); console.log(result); // returns: "Hello World" result = str.slice(0, -1); console.log(result); |
4. Using a loop and an array
We can use a for loop to iterate over each character in the string and checks if it matches one of the characters that we want to remove. If it does, it skips it and does not add it to an array. If it does not, it adds it to the array. Then, it joins the array into a new string. For example, if we want to remove all punctuation marks (., !) from the string "Hello World!", we can write:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
let str = "Hello World!"; // Initialize an empty array let arr = []; // Loop over each character in the string for (let i = 0; i < str.length; i++) { // Get the current character let char = str[i]; // Check if the character is not one of the punctuation marks if (char !== "." && char !== "!") { // Add the character to the array arr.push(char); } } // Join the array into a new string let result = arr.join(""); // result is "Hello World" console.log(result); |
That’s all about removing specific characters from a string in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)