Replace all Occurrences of a Substring in a String in JavaScript Last Updated : 08 Nov, 2024 Comments Improve Suggest changes 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 Replace all Occurrences of a Substring in 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 Javascript Program To Check If A String Is Substring Of Another Given two strings s1 and s2, find if s1 is a substring of s2. If yes, return the index of the first occurrence, else return -1. Examples :Â Input: s1 = "for", s2 = "geeksforgeeks" Output: 5 Explanation: String "for" is present as a substring of s2. Input: s1 = "practice", s2 = "geeksforgeeks" Output 2 min read How to check a string is entirely made up of the same substring in JavaScript ? We are given a string and the task is to determine whether the string is made up of the same substrings or not. 1. Using Regular Expression with test() method Initialize a string to the variable.Use the test() method to test a pattern in a string.The test() method returns true if the match is found, 3 min read JavaScript Program to Split a String into a Number of Sub-Strings Given a string, the task is to split the string into number of sub-strings using JavaScript. This task is common in scenarios where data needs to be segmented or processed in chunks. This article provides a flexible solution that can divide a given string into a desired number of smaller substrings, 2 min read Split a String into an Array of Words in JavaScript JavaScript allows us to split the string into an array of words using string manipulation techniques, such as using regular expressions or the split method. This results in an array where each element represents a word from the original string. 1. Using split() MethodUsing split() method with a chos 5 min read Like