Trusted answers to developer questions

How to replace a string in JavaScript

Get the Learn to Code Starter Pack

Break into tech with the logic & computer science skills you’d learn in a bootcamp or university — at a fraction of the cost. Educative's hand-on curriculum is perfect for new learners hoping to launch a career.

The replace() method searches a string for a specified substring (of a regular expression) and replaces the specified substring with the new string. ​

svg viewer

Syntax

Below is the syntax for the replace() function in JavaScript:

svg viewer

Code

The code snippet below illustrates the usage of the replace() function in JavaScript:

// original string
var str = "I am working in Python"
console.log(str)
// replace function
str = str.replace("Python","JavaScript")
console.log(str)

RELATED TAGS

replace
string
javascript
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?