RegExp.prototype[@@replace]() | regexp replace
The[@@replace]()methodreplacessomeorallmatchesofathispatterninastringbyareplacement,andreturnstheresultofthereplacementasanewstring.Thereplacementcanbeastringorafunctiontobecalledforeachmatch.ThesourceforthisinteractiveexampleisstoredinaGitHubrepository.Ifyoudliketocontributetotheinteractiveexamplesproject,pleaseclonehttps://github.com/mdn/interactive-examples[1]andsendusapullrequest.Syntaxregexp[Symbol.replace](str,newSubStr|function)ParametersstrAString[2]thatisatargetofthereplacement.newS...
The [@@replace]() method replaces some or all matches of a this pattern in a string by a replacement, and returns the result of the replacement as a new string. The replacement can be a string or a function to be called for each match.
The source for this interactive example is stored in a GitHub repository. If youd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples[1] and send us a pull request.
Syntax regexp[Symbol.replace](str, newSubStr|function) Parameters str A String[2] that is a target of the replacement. newSubStr (replacement) The String[3] that replaces the substring. A number of special replacement patterns are supported; see the Specifying a string as a parameter[4] section in String.prototype.replace()[5] page. function (replacement) A function to be invoked to create the new substring. The arguments supplied to this function are described in the Specifying a function ...