JavaScript - Remove all Occurrences of a Character in JS String Last Updated : 21 Nov, 2024 Summarize Suggest changes Share Like Article Like Report These are the following ways to remove all occurrence of a character from a given string: 1. Using Regular ExpressionUsing a regular expression, we create a pattern to match all occurrences of a specific character in a string and replace them with an empty string, effectively removing that character. JavaScript let str = "Geeks-for-Geeks"; let ch = "-"; let regex = new RegExp(ch, 'g'); let res = str.replace(regex, ''); console.log(res); OutputGeeksforGeeks 2. Using the split() and join() Methods In this approach, Using the split() method, we break a string into an array at each occurrence of a specified character. Then, with the join method, we reconstruct the array into a new string, effectively removing that character. JavaScript let str = "Geeks-for-Geeks"; let ch = "e"; let res = str.split(ch).join(''); console.log(res); OutputGks-for-Gks 3. Using for..in loopThe for...in loop iterates through characters of the input string. If a character doesn't match the specified one, it's appended to a new string, effectively removing the specified character. JavaScript let str = "Geeks"; let ch = "e"; let res = ''; for (let idx in str) { if (str[idx] !== ch) { res += str[idx]; } } console.log(res); OutputGks 4. Using String.prototype.replace() MethodUsing String.prototype.replace() with a function involves passing a regular expression to match the character to be removed globally. Inside the replace function, return an empty string to remove each occurrence of the character. JavaScript let str = "Geeks"; let ch = "e"; let res = str.replace(new RegExp(ch, 'g'), () => ''); console.log(res); OutputGks 5. Using Array.prototype.filter() MethodThe input string is converted into an array of characters using the split('') method. Then, the filter() method is used to create a new array excluding the specified character. Finally, the join('') method is used to reconstruct the array back into a string. JavaScript let str = "Geeks"; let ch = "e"; let res = str.split('').filter(c => c !== ch).join(''); console.log(res); OutputGks Advertise with us Next Article JavaScript- Remove Last Characters from JS String P parzival_op Follow Similar Reads JavaScript Program to Count the Occurrences of a Specific Character in a String In this article, we will see how to count the frequency of a specific character in a string with JavaScript. Counting the frequency of a specific character in a string is a common task in JavaScript. Example: Input : S = âgeeksforgeeksâ and c = âeâOutput : 4Explanation: âeâ appears four times in str 3 min read JavaScript- Delete all Occurrences in a JS Array These are the following ways to remove all Occurrences of an element in a JS Array: 1. Using filter() method( Simple and Easy for Long Arrays)The filter() method creates a new array with all elements that passed the test implemented by the given callback function.JavaScriptfunction fun(a, e) { if (! 2 min read JavaScript- Remove Last Characters from JS String These are the following ways to remove first and last characters from a String:1. Using String slice() MethodThe slice() method returns a part of a string by specifying the start and end indices. To remove the last character, we slice the string from the start (index 0) to the second-to-last charact 2 min read JavaScript Program to Print All Duplicate Characters in a String In this article, we will learn how to print all duplicate characters in a string in JavaScript. Given a string S, the task is to print all the duplicate characters with their occurrences in the given string. Example: Input: S = âgeeksforgeeksâOutput:e, count = 4g, count = 2k, count = 2s, count = 2Ta 5 min read JavaScript Program to Remove Last Character from the String In this article, we will learn how to remove the last character from the string in JavaScript. The string is used to represent the sequence of characters. Now, we will remove the last character from this string. Example: Input : Geeks for geeks Output : Geeks for geek Input : Geeksforgeeks Output : 3 min read JavaScript Program to Swap Characters in a String In this article, We'll explore different approaches, understand the underlying concepts of how to manipulate strings in JavaScript, and perform character swaps efficiently. There are different approaches for swapping characters in a String in JavaScript: Table of Content Using Array ManipulationUsin 6 min read Article Tags : JavaScript Web Technologies javascript-string JavaScript-DSA JavaScript-Program +1 More Like