How to replace a string in JavaScript
The replace() method searches a string for a specified substring (of a regular expression) and replaces the specified substring with the new string.
Syntax
Below is the syntax for the replace() function in JavaScript:
Code
The code snippet below illustrates the usage of the replace() function in JavaScript:
// original stringvar str = "I am working in Python"console.log(str)// replace functionstr = str.replace("Python","JavaScript")console.log(str)
Free Resources
Copyright ©2026 Educative, Inc. All rights reserved