JavaScript String replace() Method6 Sept 2025 | 4 min read The JavaScript string replace() method is used to replace a part of a given string with a new substring. This method searches for a specified regular expression in a given string and then replaces it if a match occurs. ![]() We can use the global search modifier with the replace() method to replace all the matched elements otherwise the method replaces only the first match. JavaScript also provides an ignore flag to make the method case-insensitive. SyntaxThe replace() method is represented by the following syntax: ParameteroriginalStr - It represents the string to be searched and replaced. newStr - It represents the new string that replaces the searched string. ReturnIt returns a new string with the specified replacement. JavaScript String replace() Method ExamplesLet's see some examples of the replace() method. Example 1: Replacing a substringLet's see a simple example to replace a substring. ExampleExecute NowOutput: ScriptTech Explanation: In the upper example, we create a variable named str using let. We replaced a string using the .replace() method. Here, JavaScript replaced TpointTech with ScriptTech. Example 2: Replacing a regular expression using the global search modifierIn this example, we will replace a regular expression using the global search modifier. ExampleExecute NowOutput: Learn AngularJS on TpointTech. AngularJS is a well-known JavaScript framework. Explanation: In the upper example, we have defined a variable named str using let and assigned it a string value. We have replaced Node.js with AngularJS using the global search modifier. Example 3: Replacing a regular expression without using the global searchIn this example, we will replace a regular expression without using global search. ExampleExecute NowOutput: Learn AngularJS on TpointTech. Node.js is a well-known JavaScript framework. Explanation: In the upper example, we define a variable named str using let. We replaced a regular expression without using global search. It will replace only the first match. Example 4: Case-Sensitivity of the replace() MethodIn JavaScript, the replace() method is case-sensitive. In this example, we will see how the replace() method is case-sensitive. ExampleExecute NowOutput: Learn Node.js on TpointTech. Node.js is a well-known JavaScript framework. Explanation: In the above example, we have defined a variable named str. We have tried to replace Node.js with AngularJS but instead of writing Node.js, we have written Node.JS which makes it a different word so the replace() method will not replace Node.js with AngularJS. This shows the case-sensitive behaviour of the replace() method. Example 5: Ignoring the case-sensitive behaviour of the replace() methodWe can ignore the case-sensitive behaviour of the replace() method by using the ignore flag modifier. Let's understand with the help of an example. ExampleExecute NowOutput: Learn AngularJS on TpointTech. AngularJS is a well-known JavaScript framework. Explanation: In the upper example, we have declared a variable named str using let. We have used the ignore case flag, so the replace() method matches 'Node.js' regardless of case-sensitive behaviour and printed the output to the console. ConclusionThe replace() method is a method of a string that is very helpful for replacing a string in JavaScript. It is one of the most frequently used methods in string. In this article, we have covered the replace() method and have seen various examples of it. FAQs:1. What does the replace() method do to a string? In JavaScript, we make use of the replace() method in a string to replace a substring or character with another string. After the replacement, it returns a new string that contains the replaced substring or character. 2. When a replace() method is used with a string, is the string modified? In JavaScript, the replace() method does not make any changes to the original string. Rather, it gives us a new string in which all the occurrences of the old value are replaced by the new value. 3. Does string replace all instances? If we replace a value, only the first instance will be replaced. If we want to replace all instances, we have to use a regular expression with the g modifier set. 4. Can we replace the last comma from a string in JavaScript using the replace() method? Yes, we can replace the last comma from a string using the replace() method along with a regular expression pattern. Next TopicJavaScript String |
We request you to subscribe our newsletter for upcoming updates.