ReplaceAll JavaScript Example: A Complete Guide

The JavaScript replaceAll() method, introduced in ECMAScript 2021, is a helpful tool for string manipulation. Unlike its predecessor, replace(), it replaces all occurrences of a substring, not just the first one. Let's delve into this method, exploring its syntax, how it works, and practical use cases with examples.

Dynamically Load & Replace CSS
Dynamically Load & Replace CSS

Before we dive in, let's ensure we're working with a modern JavaScript environment that supports replaceAll(). This includes browsers like Chrome 87+, Edge 83+, and Firefox 89+, as well as Node.js 14.6+. With that covered, let's get started.

JavaScript Array reduce() Method
JavaScript Array reduce() Method

Understanding the replaceAll() Method

The replaceAll() method replaces all occurrences of a specified value with a new value within a string. It takes two required parameters: the match value and the replacement value.

Java script string method
Java script string method

Syntax-wise, it looks like this: str.replaceAll(find, replace). Here, find is the match value, and replace is the replacement value. It returns a new string with all matches replaced.

Basic Syntax and Usage

JavaScript replaceWith function - CodingTute
JavaScript replaceWith function - CodingTute

The basic usage of replaceAll() is straightforward. For instance, suppose we have a string and we want to replace all occurrences of 'world' with 'JavaScript'.

let str = 'Hello world! How is the world treating you today, world?'; let newStr = str.replaceAll('world', 'JavaScript'); console.log(newStr); // Outputs: 'Hello JavaScript! How is the JavaScript treating you today, JavaScript?'

Using replaceAll() with Regular Expressions

JavaScript Recursion Simplified
JavaScript Recursion Simplified

replaceAll() also supports regular expressions. For example, to replace all whitespace characters with a single space, one might use:

let str = ' Hello world! How is the world treating you today, world? '; let newStr = str.replaceAll(/\s+/g, ' '); console.log(newStr); // Outputs: 'Hello world! How is the world treating you today, world?'

Compare replace() vs replaceAll()

How to Replace All Occurrences of a String in JavaScript
How to Replace All Occurrences of a String in JavaScript

The main difference between replace() and replaceAll() is that replace() only replaces the first occurrence of the match, while replaceAll() replaces all occurrences.

For instance, consider let str = 'Hello world! How is the world treating you today, world?';. The following will only replace the first 'world': let newStr = str.replace('world', 'JavaScript');.

Javascript Fundamental
Javascript Fundamental
an info sheet with different types of web pages
an info sheet with different types of web pages
JavaScript calculator projet
JavaScript calculator projet
JavaScript Basics | Chapter 1 📖 | Part 1
JavaScript Basics | Chapter 1 📖 | Part 1
the world's most famous lines are shown in this diagram, which shows how many different
the world's most famous lines are shown in this diagram, which shows how many different
JavaScript Full Notes Every Beginner Must Save 💡
JavaScript Full Notes Every Beginner Must Save 💡
JavaScript Return Values: Get the Most Out of Your Functions 🚀
JavaScript Return Values: Get the Most Out of Your Functions 🚀
8 Essential JavaScript Array Methods Visual Guide
8 Essential JavaScript Array Methods Visual Guide
JavaScript String Methods Cheat Sheet 📘✨
JavaScript String Methods Cheat Sheet 📘✨
JavaScript Manipulating Arrays 🚀 Follow for more 💡🌐🚀
JavaScript Manipulating Arrays 🚀 Follow for more 💡🌐🚀
JavaScript study guide for fast learning
JavaScript study guide for fast learning
an image of a computer program with numbers and symbols
an image of a computer program with numbers and symbols
JavaScript Conditional Operators: Write Smarter Code with ? : 🚀
JavaScript Conditional Operators: Write Smarter Code with ? : 🚀
JavaScript Cheat Sheet You Must Save | Beginner Friendly Guide
JavaScript Cheat Sheet You Must Save | Beginner Friendly Guide
a poster with different types of projects and instructions for students to use in the classroom
a poster with different types of projects and instructions for students to use in the classroom
JavaScript | Chapter 2 📖| Part 1 ✨
JavaScript | Chapter 2 📖| Part 1 ✨
JavaScript Increment & Decrement Operators: Simplify Your Code 🔄
JavaScript Increment & Decrement Operators: Simplify Your Code 🔄
JavaScript - Array Methods - Theory
JavaScript - Array Methods - Theory
Free JavaScript Book
Free JavaScript Book
Remove Line Breaks In Javascript
Remove Line Breaks In Javascript

Upgrade to replaceAll()

If you have existing code using replace() and you want to upgrade to replaceAll(), remember that replaceAll() expects the second argument to be the replacement string, not a function.

So, upgrade like this: from str.replace(/old/g, function() {...}); to str.replaceAll('old', 'new');.

In essence, replaceAll() offers a simpler, more intuitive way to replace all occurrences of substrings, aligning better with principles of JavaScript's functional programming and making code more predictable and easier to read.

And there you have it, a comprehensive dive into the JavaScript replaceAll() method. With this tool in your kit, you're better equipped to handle string manipulations in your projects. Happy coding and stay curious!

Related Articles

Window Event In Javascript W3schools Data Validation In Javascript W3schools Window Close Javascript W3schools Javascript Regex Test Example Leap Year In C Programming Vanilla Javascript Tutorial W3schools Javascript Browser Window Object Difference Between Let And Var In Javascript W3schools W3schools Javascript Do While Loop Javascript Test Regex Pattern