How to Replace All Occurrences of a String in JavaScript? Last Updated : 19 Nov, 2024 Comments Improve Suggest changes Like Article Like Report Here are different approaches to replace all occurrences of a string in JavaScript.1. Using string.replace() MethodThe string.replace() method is used to replace a part of the given string with another string or a regular expression. The original string will remain unchanged.Syntaxstr.replace(replacing_string, Original_string); JavaScript // Origin String const str = 'Welcome GeeksforGeeks, Welcome geeks'; // Replace all occurrence of Welcome with Hello const newString = str.replace(/Welcome/gi, 'Hello'); // Display the result console.log(newString); OutputHello GeeksforGeeks, Hello geeks 2. Using String split() and join() MethodsWe can split the strings of text with the split() method and join strings using the replace characters with the join() method. JavaScript // Original String const str = 'Welcome GeeksforGeeks, Welcome geeks'; // Replace all occurrence of Welcome with Hello const newString = str.split('Welcome').join('Hello'); // Display the result console.log(newString); OutputHello GeeksforGeeks, Hello geeks 3. Using replaceAll() MethodThe replaceAll() method is used to replace all the matches of a string with a specified string or a regular expression. The original string is left unchanged after this operation. JavaScript // Original String const str = 'Welcome GeeksforGeeks, Welcome geeks'; // Replace all occurrences of Welcome with Hello const newString = str.replaceAll("Welcome", "Hello"); // Display the result console.log(newString); OutputHello GeeksforGeeks, Hello geeks 4. Using Regular ExpressionTo replace all occurrences of a string in JavaScript using a regular expression, we can use the regular expression with the global (g) Flag. JavaScript const str = 'Welcome GeeksforGeeks, Welcome geeks'; const searchString ="Welcome"; const replacementString ="Hello"; let regex = new RegExp(searchString, 'g'); let replacedString = str.replace(regex, replacementString); console.log(replacedString); OutputHello GeeksforGeeks, Hello geeks 5. Using reduce() Method We can split the string into an array of substrings using the split() method, and then we use the reduce() method to concatenate the substrings, inserting the replacement string where the original substring occurs. This method gives us fine control over the replacement process and can be useful for more complex string manipulations. JavaScript // Original String const str = 'Welcome GeeksforGeeks, Welcome geeks'; const searchString = "Welcome"; const replacementString = "Hello"; // Replace all occurrences of Welcome with Hello const newString = str.split(searchString).reduce((acc, current, index, array) => { return acc + current + (index < array.length - 1 ? replacementString : ''); }, ''); // Display the result console.log(newString); OutputHello GeeksforGeeks, Hello geeks Comment More infoAdvertise with us Next Article How to Replace All Occurrences of a String in JavaScript? V vkash8574 Follow Improve Article Tags : JavaScript Web Technologies javascript-string JavaScript-DSA JavaScript-Questions +1 More Similar Reads Replace all Occurrences of a Substring in a String in JavaScript Given a String, the task is to replace all occurrences of a substring using JavaScript. The basic method to replace all substring of a string is by using string.replaceAll() method.The string.replaceAll() method is used to search and replace all the matching substrings with a new string. We can use 2 min read How to get nth occurrence of a string in JavaScript ? In this article, the task is to get the nth occurrence of a substring in a string with the help of JavaScript. We have many methods to do this some of which are described below:Approaches to get the nth occurrence of a string:Table of Content Using split() and join() methodsUsing indexOf() methodUsi 5 min read Replace Duplicate Occurrence in a String in JavaScript JavaScript String is a sequence of characters, typically used to represent text. It is useful to clean up strings by removing any duplicate characters. This can help normalize data and reduce unnecessary bloat. There are several ways to replace duplicate occurrences in a given string using different 3 min read JavaScript - How To Count String Occurrence in String? Here are the various methods to count string occurrence in a string using JavaScript.1. Using match() Method (Common Approach)The match() method is a simple and effective way to count occurrences using a regular expression. It returns an array of all matches, and the length of the array gives the co 3 min read How to replace all dots in a string using JavaScript ? We will replace all dots in a string using JavaScript. There are multiple approaches to manipulating a string in JavaScript. Table of Content Using JavaScript replace() MethodUsing JavaScript Split() and Join() Method Using JavaSccript reduce() Method and spread operatorUsing JavaScript replaceAll() 4 min read How to replace all occurrences of a string using Vue.js filters ? In this article, we are going to learn how to replace all occurrences of a particular word with some other word using filters in VueJS. Vue is a progressive framework for building user interfaces. Filters are a functionality provided by Vue components that let you apply formatting and transformation 2 min read How to Lowercase a String in JavaScript ? In JavaScript, converting a string to lowercase means transforming all the characters in the string to their lowercase equivalents. This operation is often used to ensure consistent comparison or formatting of text. Below are the approaches used to Lowercase a string in JavaScript: Table of Content 2 min read How to Search a String for a Pattern in JavaScript ? Here are two ways to search a string for a pattern in JavaScript.1. Using string search() MethodThe JavaScript string search() method is used to serch a specified substing from the given string and regular expression. It returns the index of the first occurrence of the pattern or -1 if the pattern i 2 min read How to modify a string in JavaScript ? JavaScript strings are used for storing and manipulating text. It can contain zero or more characters within quotes and its indexing starts with 0. Strings are defined as an array of characters. In Javascript, we can also convert strings to a Character array. Representing a String: 1. Using double 3 min read How to JSON Stringify an Array of Objects in JavaScript ? In JavaScript, the array of objects can be JSON stringified for easy data interchange and storage, enabling handling and transmission of structured data. The below approaches can be utilized to JSON stringify an array of objects.Table of ContentUsing JSON.stringify with a Replacer Function Using a C 3 min read Like