JavaScript - Remove all Occurrences of a Character in JS String Last Updated : 21 Nov, 2024 Comments Improve Suggest changes 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 Comment More infoAdvertise with us Next Article JavaScript - Remove all Occurrences of a Character in JS String P parzival_op Follow Improve Article Tags : JavaScript Web Technologies javascript-string JavaScript-DSA JavaScript-Program +1 More 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 JavaScript - How to Replace Multiple Characters in a String? Here are the various methods to replace multiple characters in a string in JavaScript.1. Using replace() Method with Regular ExpressionThe replace() method with a regular expression is a simple and efficient way to replace multiple characters.JavaScriptconst s1 = "hello world!"; const s2 = s1.replac 3 min read JavaScript - String Contains Only Alphabetic Characters or Not Here are several methods to check if a string contains only alphabetic characters in JavaScriptUsing Regular Expression (/^[A-Za-z]+$/) - Most USedThe most common approach is to use a regular expression to match only alphabetic characters (both uppercase and lowercase).JavaScriptlet s = "HelloWorld" 2 min read JavaScript - How to Get the Last N Characters of a String? Here are the different methods to get the last N characters of a string in JavaScript.1. Using slice() MethodThe slice() method is the most commonly used approach for extracting the last N characters, thanks to its simplicity and support for negative indices.JavaScriptconst getChar = (s, n) => s. 2 min read JavaScript Program to Find Kâth Non-Repeating Character in String The K'th non-repeating character in a string is found by iterating through the string length and counting how many times each character has appeared. When any character is found that appears only once and it is the K'th unique character encountered, it is returned as the result. This operation helps 6 min read Like