Replace all Occurrences of a Substring in a String in JavaScript Last Updated : 08 Nov, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 the regular expression as a pattern to find all the matching substrings.Syntaxstring.replaceAll(sunstring, replacement); JavaScript let str = "GEEKSforGEEKS"; let substring = "GEEKS"; let replacement = "Geeks"; // Replace all occurrences of substring let updatedStr = str.replaceAll(substring, replacement); // Updated string console.log("Updated String:", updatedStr); OutputUpdated String: GeeksforGeeks Table of ContentUsing String replace() Method with Regular ExpressionUsing indexOf() and slice() MethodsUsing String replace() Method with Regular ExpressionThe string replace() method is used to search and replace a substings with a new string. We can use the regular expression as a pattern to find all the matching substrings.Syntaxstring.replace(substring, replacement); JavaScript let str = "GEEKSforGEEKS"; let substring = "GEEKS"; let replacement = "Geeks"; let updatedStr = str.replace(new RegExp(substring, "g"), replacement); // Updated string console.log("Updated String:", updatedStr); OutputUpdated String: GeeksforGeeks Using indexOf() and slice() MethodsThe indexOf() method finds the index of the first occurrence of the substring. Next, we can break the string from that index using str.slice() and insert the replacement string to get the required output.str.indexOf(searchValue, startIndex); string.slice(startingIndex, endingIndex); JavaScript let str = "GEEKSforGEEKS"; let substring = "GEEKS"; let replacement = "Geeks"; while (str.indexOf(substring) !== -1) { let index = str.indexOf(substring); str = str.slice(0, index) + replacement + str.slice(index + substring.length); } // Updated string console.log("Updated String:", str); OutputUpdated String: GeeksforGeeks Comment More infoAdvertise with us Next Article How to Replace All Occurrences of a String in JavaScript? P pranay0911 Follow Improve Article Tags : JavaScript Web Technologies Similar Reads How to Replace All Occurrences of a String in JavaScript? 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(replac 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 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 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 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 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 Like