JavaScriptStrings

String.prototype.replace()

Returns a new string in which the first match of a pattern (or all matches, with the g flag) has been replaced with other text.

Returns
A new string after the replacement. If nothing matched, the string comes back unchanged.
Changes the original
No
On this page

Example

#
JavaScript
const message = 'Welcome to the HTML course. HTML is easy!'

console.log(message.replace('HTML', 'CSS'))
console.log(message.replace(/HTML/g, 'CSS'))
console.log(message)
Console
Welcome to the CSS course. HTML is easy!
Welcome to the CSS course. CSS is easy!
Welcome to the HTML course. HTML is easy!

Definition and usage

#

The replace() method looks for a pattern and returns a new string with the matched piece swapped out. When the pattern is plain text, only the first occurrence gets replaced. The original string stays unchanged, so you need to save the result to a variable.

The pattern can be a regular expression. With the g flag, every match gets replaced, and with the i flag, letter case stops mattering. In the replacement text, special sequences insert pieces of the match: $& is the whole match, and $1, $2 are the capture groups in order.

Instead of text, you can pass a function. It gets called for every match, and whatever it returns ends up in the result. That lets you do things like recalculate numbers found in text or swap words based on a dictionary.

Syntax

#
Syntax
string.replace(pattern, replacement)

Parameters

#
  • pattern

    string | RegExp

    The text or regular expression to find. Plain text replaces only the first occurrence, while an expression with the g flag replaces them all.
  • replacement

    string | function

    The text inserted in place of the match (it can contain $&, $1, and so on), or a function that receives the match and its groups and returns the replacement text.

More examples

#
Groups in the replacement text
JavaScript
const date = '2026-09-28'
const formatted = date.replace(/(\d{4})-(\d{2})-(\d{2})/, '$3.$2.$1')

console.log(formatted)
Console
28.09.2026
A function as the replacement
JavaScript
const receipt = 'Course 100 dollars, e-book 40 dollars'
const halfPrice = receipt.replace(/(\d+) dollars/g, (match, amount) => `${amount / 2} dollars`)

console.log(halfPrice)
Console
Course 50 dollars, e-book 20 dollars
Case does not matter
JavaScript
const review = 'Great course! JAVASCRIPT and javascript are the same thing.'

console.log(review.replace(/javascript/gi, 'JavaScript'))
Console
Great course! JavaScript and JavaScript are the same thing.

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • To replace every occurrence of plain text, use replaceAll() or an expression with the g flag. Plain replace("a", "b") only changes the first one.
  • The $ character has special meaning in the replacement text. Write a literal dollar sign as $$, or return it from a function instead.
  • When building a regular expression from user-supplied text, watch out for special characters like ., *, or ?. For plain text, replaceAll() with a string is safer.

Related entries

#

Spotted a mistake or missing an example? Write to us.