.replace(old,new)
Description
The .replace() method replaces occurrences of text in a string. It supports both literal string replacement and regular expression patterns.
Parameters
old: The text to replace. Can be either:A literal string to match exactly
A regex pattern in the format "/pattern/flags"
new: The text to replace matches with
Usage
Basic String Replacement
text = "hello world"
result = text.replace("hello", "hi")
// returns "hi world"Regular Expression Replacement
Regex Flags
When using regex patterns, you can add flags after the closing slash:
g- global (replace all matches)i- case insensitivem- multiline
Examples
Last updated
Was this helpful?