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 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 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 replace a portion of strings with another value in JavaScript ? In JavaScript, replacing a portion of strings with another value involves using the `replace()` method or regular expressions. This process is essential for text manipulation tasks like formatting, sanitization, or dynamic content generation in web development and data processing applications. We ca 3 min read JavaScript - How to Globally Replace a Forward Slash in a String? Here are the various methods to globally replace a forward slash in the JavaScript string.1. Using replace() Method with a Regular ExpressionThe replace() method is commonly used with a regular expression to replace all occurrences of a forward slash.JavaScriptconst s1 = "path/to/some/resource"; con 2 min read How to Add Backslash in JSON String JavaScript ? In JavaScript, adding a backslash to a JSON string is important to properly escape special characters, ensuring the integrity and correctness of the JSON format for data processing and storage. Table of Content Using JSON.parse() and JSON.stringify()Using for LoopUsing Array.prototype.map() and Stri 2 min read How to Remove Spaces From a String using JavaScript? These are the following ways to remove space from the given string:1. Using string.split() and array.join() MethodsJavaScript string.split() method is used to split a string into multiple sub-strings and return them in the form of an array. The join() method is used to join an array of strings using 2 min read Like