You can specify a function as the second parameter. In this case, the function will be invoked after the match has been performed. The function's result (return value) will be used as the replacement string. (Note: The above-mentioned special replacement patterns do not apply in this case.)
Note that if the first argument of an replaceAll()
invocation is a RegExp
object or regular expression literal, the function will be invoked multiple times.
The arguments to the function are as follows:
(The exact number of arguments depends on whether the first argument is a RegExp
object—and, if so, how many parenthesized submatches it specifies.)