Tin mới
JavaScript: Scripting language
Web APIs: Programming interfaces
Structuring content with HTML module
Dynamic scripting with JavaScript module
Can be a string or an object with a Symbol.replace method — the typical example being a regular expression. Any value that doesn't have the Symbol.replace method will be coerced to a string.
RegExp.prototype[Symbol.replace]()
This method does not mutate the string value it's called on. It returns a new string.
Note: See the regular expression guide for more explanations about regular expressions.
The following script switches the words in the string. For the replacement text, the script uses capturing groups and the $1 and $2 replacement patterns.
Using an inline function that modifies the matched characters
In this example, all occurrences of capital letters in the string are converted to lower case, and a hyphen is inserted just before the match location. The important thing here is that additional operations are needed on
Suppose we want to create a replacer that appends the offset data to every matched string. Because the replacer function already receives the offset parameter, it will be trivial if the regex is statically known.
This page was last modified on Apr 12, 2026 by MDN contributors.