JavaScript String replaceAll() - Replace All Matches

Updated on 15 May, 2025
replaceAll() header image

Introduction

The replaceAll() method in JavaScript is designed to search for all occurrences of a substring in a string and replace them with a specified replacement string. This method offers a straightforward way to manipulate text data by ensuring that all instances of the search string are replaced, providing consistency throughout the altered data.

In this article, you will learn how to use the replaceAll() method in various contexts. Explore practical examples of applying replaceAll() with strings and regular expressions in both browser-based and Node.js JavaScript environments. Learn how this utility can be used to clean data, make bulk modifications to user input, and adapt existing text to new formats.

Understanding the Basics of the JavaScript replaceAll() Method

Basic Usage of replaceAll()

  1. Start with a simple string that contains repeated words or characters.

  2. Apply the replaceAll() method to replace all occurrences of the target substring.

    javascript
    let message = "Hello World. World is vast.";
    let newMessage = message.replaceAll("World", "Earth");
    console.log(newMessage);
    

    This code replaces every instance of "World" with "Earth" in the message string, outputting "Hello Earth. Earth is vast.".

Case Sensitivity in replaceAll()

  1. Appreciate that replaceAll() is case-sensitive.

  2. Modify the method to incorporate a case-insensitive replacement.

    javascript
    let text = "Favorite Color. FAVORITE COLOR. FaVoRiTe CoLoR.";
    let newText = text.replaceAll(/favorite color/gi, "chosen color");
    console.log(newText);
    

    In this example, the regular expression /favorite color/gi ensures that all case variations of "Favorite Color" are replaced with "chosen color", demonstrating how regular expressions enhance the basic functionality of replaceAll().

Important: When using a regular expression with replaceAll(), it must include the global (g) flag—otherwise, a TypeError will be thrown.

Practical Use Cases for JavaScript replaceAll()

Handling User Input for Consistency

  1. Imagine receiving user input with mixed formatting.

  2. Utilize replaceAll() to standardize or clean this data.

    javascript
    let userInput = "Email: example@Domain.com";
    let sanitizedInput = userInput.replaceAll("Email: ", "").trim().toLowerCase();
    console.log(sanitizedInput);
    

    This snippet effectively strips a prefix and standardizes the email address to lowercase, illustrating how replaceAll() can be part of input sanitization processes.

Dynamic Text Replacement in Templates

  1. Consider a scenario with a text template that includes placeholders.

  2. Use replaceAll() to substitute placeholders with dynamic values.

    javascript
    let template = "Dear [name], your order [order_id] is ready.";
    let personalized = template.replaceAll("[name]", "Alice").replaceAll("[order_id]", "12345");
    console.log(personalized);
    

    This code dynamically replaces the placeholder tokens [name] and [order_id] with specific values, making it a useful method for creating personalized content from templates.

    The replaceAll() method can be further used with regular expressions to match more dynamic patterns, such as multiple identical placeholders or variable spacing.

Conclusion

The replaceAll() function in JavaScript is a versatile and essential tool for text manipulation, enabling developers to replace all occurrences of a given substring within a string. Whether for data sanitization, bulk text modifications, or dynamic content generation, this method offers a reliable and powerful option for managing and manipulating strings across diverse applications. By mastering the replaceAll() method, you fortify your toolkit with the ability to handle complex text processing tasks efficiently.

Comments

No comments yet.