In JavaScript, the replace() method is used to find and replace text within a string. It works by locating the first occurrence of a specified substring (or regular expression) and replacing it with a new substring.

Unlike some other string methods, the replace() method does not mutate the original string. Instead, it returns a new string with the replacement applied.

Basic Usage

To use the replace() method, you pass two arguments: the substring you want to replace and the replacement substring. For example:

'JavaScript'.replace('Java', 'Type') //'TypeScript'

In this case, the first occurrence of “Java” in the string “JavaScript” is replaced with “Type”, resulting in the string “TypeScript”.

Using Regular Expressions

In addition to simple substrings, you can also pass a regular expression as the first argument to the replace() method. This enables more advanced search patterns. For example:

'JavaScript'.replace(/Java/, 'Type') //'TypeScript'

Here, the regular expression /Java/ is used to find the first occurrence of “Java” in the string “JavaScript”, which is then replaced with “Type”.

To replace all occurrences of a substring (or regular expression), you can specify the global (/g) option in the regular expression. For example:

'JavaScript JavaX'.replace(/Java/g, 'Type') //'TypeScript TypeX'

The /g option ensures that all instances of “Java” are replaced with “Type”, resulting in the string “TypeScript TypeX”.

Using a Function as the Second Parameter

Instead of providing a replacement substring, you can pass a function as the second parameter to the replace() method. This function will be invoked when a match is found (or for every match found, if using a global regular expression). The function will receive several arguments:

  • The string that matches the pattern
  • The position within the string where the match occurred
  • The original string itself

The return value of the function will replace the matched part of the string. Here’s an example:

'JavaScript'.replace(/Java/, (match, index, originalString) => {
 console.log(match, index, originalString)
 return 'Test'
}) //TestScript

In this case, the function is passed the matching substring “Java”, the index where the match occurred (0 in this case), and the original string “JavaScript”. The function returns “Test”, so the resulting string is “TestScript”.

The same approach can also be used with regular strings, not just regular expressions:

'JavaScript'.replace('Java', (match, index, originalString) => {
 console.log(match, index, originalString)
 return 'Test'
}) //TestScript

Using Capturing Groups

If your regular expression contains capturing groups (expressions wrapped in parentheses), the values captured by these groups will be passed as additional arguments after the match parameter. Here’s an example:

'2015-01-02'.replace(/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/, (match, year, month, day, index, originalString) => {
 console.log(match, year, month, day, index, originalString)
 return 'Test'
}) //Test

In this case, the regular expression uses named capturing groups to extract the year, month, and day from a date string. The function receives these captured values as additional arguments, along with the match, index, and original string.