GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • JS Tutorial
  • Web Tutorial
  • A to Z Guide
  • Projects
  • OOP
  • DOM
  • Set
  • Map
  • Math
  • Number
  • Boolean
  • Exercise
  • Google Chrome
  • Microsoft Edge

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — JavaScript String replaceAll() Method - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • JS Tutorial
  • Web Tutorial
  • A to Z Guide
  • Projects
  • OOP
  • DOM
  • Set
  • Map
  • Math
  • Number
  • Boolean
  • Exercise

JavaScript String replaceAll() Method

Last Updated : 23 Jul, 2025

The replaceAll() method in JavaScript is used to replace all occurrences of a specified substring or pattern with a new substring. The replaceAll() method does not change the original string.

JavaScript's replaceAll() method used for replacing all instances of a specified substring or pattern within a string with a new substring. This method comprehensively updates the string by replacing every occurrence of the target substring or pattern with the provided replacement string.

Note: The original string remains unchanged, preserving its integrity throughout the process.

Syntax:

const newString = originalString.replaceAll(regexp | substr , newSubstr | function)

Parameters:

This method accepts certain parameters defined below: 

  • regexp: It is the regular expression whose matches are replaced with the newSubstr or the value returned by the specified function.
  • substr: It defines the substrings which are to be replaced with newSubstr or the value returned by the specified function.
  • newSubstr: It is the substring that replaces all the matches of the string specified by the substr or the regular expression.
  • function: It is the function that is invoked to replace the matches with the regexp or substr.

Return Value:

Returns a String where the search value has been replaced.

Example 1: String Replace All Occurrences

The function gfg() takes a string "Geeks or Geeks", replaces all occurrences of "or" with "for", and logs the modified string "Geeks for Geeks" to the console.

JavaScript
function gfg() {
    let string = "Geeks or Geeks";
    newString = string.replaceAll("or", "for");
    console.log(newString);
}
gfg();

Output
Geeks for Geeks

Example 2: Replace All Occurrences of "coffee" with "tea"

The function GFG() defines a regular expression /coffee/ig to match all occurrences of "coffee" case-insensitively in the string "Lets, have coffee today!". It then replaces all occurrences with "tea" and logs the modified string "Lets, have tea today!" to the console.

JavaScript
function GFG() {
    const regexp = /coffee/ig;
    let string = "Lets, have coffee today!";
    newString = string.replaceAll(regexp, "tea");
    console.log(newString);
}
GFG();

Output
Lets, have tea today!

We have a complete list of Javascript string methods, to check those please go through the Javascript String Complete Reference article.

Supported Browsers:

  • Google Chrome 85
  • Microsoft Edge 85
  • Mozilla Firefox 77
  • Opera 71
  • Safari 13.1
Comment

Explore

    JavaScript Basics
    • Introduction3 min read
    • Variables5 min read
    • Operators4 min read
    • Control Statements4 min read
    Array & String
    • Arrays6 min read
    • Array Methods4 min read
    • String5 min read
    • String Methods8 min read
    Function & Object
    • Functions4 min read
    • Function Expression3 min read
    • Function Overloading4 min read
    • Objects4 min read
    • Constructors4 min read
    OOP
    • OOP5 min read
    • Classes & Objects4 min read
    • Access Modifiers5 min read
    • Constructor4 min read
    Asynchronous JavaScript
    • Asynchronous2 min read
    • Callbacks4 min read
    • Promises4 min read
    • Event Loop3 min read
    • Async Await Fuction3 min read
    Exception Handling
    • Error and Exceptional Handling5 min read
    • Errors Throw & Try to Catch2 min read
    • Custom Errors2 min read
    • TypeError Invalid Array.prototype.sort argument1 min read
    DOM
    • DOM8 min read
    • DOM Elements3 min read
    • Custom Events4 min read
    • Addeventlistener5 min read
    Advanced Topics
    • Closure3 min read
    • Hoisting5 min read
    • Scope4 min read
    • Higher Order Functions7 min read
    • Debugging3 min read
    Courses
    • Placement 360 Course2 min read
    • MERN Full Stack Course2 min read
    • DSA and System Design Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.