replace method

basic
Published

April 20, 2024

The replace() method in JavaScript is a powerful tool for manipulating strings. It allows you to find and replace occurrences of specific substrings within a string. While seemingly simple, understanding its nuances unlocks significant potential for efficient string manipulation. This guide will look at the replace() method’s functionality, showcasing various use cases with clear code examples.

Basic String Replacement

At its core, replace() takes two arguments: the substring to be replaced and the replacement substring.

let str = "Hello world!";
let newStr = str.replace("world", "JavaScript");
console.log(newStr); // Output: Hello JavaScript!

This example replaces the first instance of “world” with “JavaScript”. Note that only the first occurrence is replaced.

Replacing All Occurrences

To replace all occurrences of a substring, you need to use a regular expression with the global flag (g).

let str = "one two three two one";
let newStr = str.replace(/two/g, "four");
console.log(newStr); // Output: one four three four one

The /two/g regular expression finds all instances of “two” and replaces them with “four”.

Using a Replacement Function

For more complex replacements, you can provide a function as the second argument to replace(). This function receives the matched substring as an argument and returns the replacement string. This allows for dynamic replacements based on the matched text.

let str = "The price is $10 and the discount is $5.";
let newStr = str.replace(/\$\d+/g, (match) => {
  let amount = parseFloat(match.substring(1));
  return `$${amount * 1.1}`; // Increase prices by 10%
});
console.log(newStr); // Output: The price is $11 and the discount is $5.5.

This example uses a regular expression to find all prices (strings starting with ‘$’ followed by one or more digits), and then a function to increase each price by 10% before returning the modified price.

Case-Insensitive Replacement

By combining regular expressions with the i flag for case-insensitive matching, you can achieve case-insensitive replacements.

let str = "Hello World! hello world!";
let newStr = str.replace(/world/gi, "JavaScript");
console.log(newStr); // Output: Hello JavaScript! hello JavaScript!

Here, both “World” and “world” are replaced with “JavaScript”.

Capturing Groups and Backreferences

Regular expressions allow you to capture parts of the matched string using parentheses (). These captured groups can then be referenced in the replacement string using $1, $2, etc.

let str = "My phone number is (123) 456-7890.";
let newStr = str.replace(/(\(\d{3}\)) (\d{3})-(\d{4})/, "$1 $2-$3"); //Removes extra space after parenthesis
console.log(newStr); // Output: (123) 456-7890.

let newStr2 = str.replace(/(\(\d{3}\)) (\d{3})-(\d{4})/, "($1) $2-$3"); //maintains extra space after parenthesis
console.log(newStr2); // Output: (123) 456-7890.

This cleverly reformats the phone number while maintaining the original structure by referencing the captured groups.

Limitations and Considerations

Remember that replace() modifies only the first match unless a global flag (g) is used with a regular expression. For more advanced string manipulation scenarios, consider using other methods like split() and join(), or dedicated libraries like Lodash.