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
Example
#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)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
#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 thegflag 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
#const date = '2026-09-28'
const formatted = date.replace(/(\d{4})-(\d{2})-(\d{2})/, '$3.$2.$1')
console.log(formatted)const receipt = 'Course 100 dollars, e-book 40 dollars'
const halfPrice = receipt.replace(/(\d+) dollars/g, (match, amount) => `${amount / 2} dollars`)
console.log(halfPrice)const review = 'Great course! JAVASCRIPT and javascript are the same thing.'
console.log(review.replace(/javascript/gi, 'JavaScript'))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 thegflag. Plainreplace("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
#- String.prototype.replaceAll()Returns a new string in which every occurrence of a pattern has been replaced with the given text or the result of a function.
- String.prototype.match()Matches a string against a regular expression. Returns an array with the match or all matches, or null.
- RegExpA regular expression: a pattern for searching, validating, and replacing pieces of text.
- String.prototype.split()Splits a string into an array of pieces based on a separator: a character, a string, or a regular expression.
- Template literalsText templates in backticks: insert values with ${} and span multiple lines.
Spotted a mistake or missing an example? Write to us.