Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
How to Replace All Occurrences of a String in JavaScript?
Next article icon

How to Replace All Occurrences of a String in JavaScript?

Last Updated : 19 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Here are different approaches to replace all occurrences of a string in JavaScript.

1. Using string.replace() Method

The string.replace() method is used to replace a part of the given string with another string or a regular expression. The original string will remain unchanged.

Syntax

str.replace(replacing_string, Original_string);
JavaScript
// Origin String const str = 'Welcome GeeksforGeeks, Welcome geeks';  // Replace all occurrence of Welcome with Hello const newString = str.replace(/Welcome/gi, 'Hello');  // Display the result console.log(newString); 

Output
Hello GeeksforGeeks, Hello geeks 

2. Using String split() and join() Methods

We can split the strings of text with the split() method and join strings using the replace characters with the join() method. 

JavaScript
// Original String const str = 'Welcome GeeksforGeeks, Welcome geeks';  // Replace all occurrence of Welcome with Hello const newString = str.split('Welcome').join('Hello');  // Display the result console.log(newString); 

Output
Hello GeeksforGeeks, Hello geeks 

3. Using replaceAll() Method

The replaceAll() method is used to replace all the matches of a string with a specified string or a regular expression. The original string is left unchanged after this operation.

JavaScript
// Original String const str = 'Welcome GeeksforGeeks, Welcome geeks';  // Replace all occurrences of Welcome with Hello const newString = str.replaceAll("Welcome", "Hello");  // Display the result console.log(newString); 

Output
Hello GeeksforGeeks, Hello geeks 

4. Using Regular Expression

To replace all occurrences of a string in JavaScript using a regular expression, we can use the regular expression with the global (g) Flag.

JavaScript
const str = 'Welcome GeeksforGeeks, Welcome geeks'; const searchString ="Welcome"; const replacementString ="Hello";  let regex = new RegExp(searchString, 'g'); let replacedString = str.replace(regex, replacementString); console.log(replacedString); 

Output
Hello GeeksforGeeks, Hello geeks 

5. Using reduce() Method

We can split the string into an array of substrings using the split() method, and then we use the reduce() method to concatenate the substrings, inserting the replacement string where the original substring occurs. This method gives us fine control over the replacement process and can be useful for more complex string manipulations.

JavaScript
// Original String const str = 'Welcome GeeksforGeeks, Welcome geeks'; const searchString = "Welcome"; const replacementString = "Hello";  // Replace all occurrences of Welcome with Hello const newString = str.split(searchString).reduce((acc, current, index, array) => {     return acc + current + (index < array.length - 1 ? replacementString : ''); }, '');  // Display the result console.log(newString); 

Output
Hello GeeksforGeeks, Hello geeks 

Next Article
How to Replace All Occurrences of a String in JavaScript?

V

vkash8574
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • javascript-string
  • JavaScript-DSA
  • JavaScript-Questions

Similar Reads

    Replace all Occurrences of a Substring in a String in JavaScript
    Given a String, the task is to replace all occurrences of a substring using JavaScript. The basic method to replace all substring of a string is by using string.replaceAll() method.The string.replaceAll() method is used to search and replace all the matching substrings with a new string. We can use
    2 min read
    Replace Duplicate Occurrence in a String in JavaScript
    JavaScript String is a sequence of characters, typically used to represent text. It is useful to clean up strings by removing any duplicate characters. This can help normalize data and reduce unnecessary bloat. There are several ways to replace duplicate occurrences in a given string using different
    3 min read
    JavaScript - How To Count String Occurrence in String?
    Here are the various methods to count string occurrence in a string using JavaScript.1. Using match() Method (Common Approach)The match() method is a simple and effective way to count occurrences using a regular expression. It returns an array of all matches, and the length of the array gives the co
    3 min read
    How to replace all dots in a string using JavaScript ?
    We will replace all dots in a string using JavaScript. There are multiple approaches to manipulating a string in JavaScript. Table of Content Using JavaScript replace() MethodUsing JavaScript Split() and Join() Method Using JavaSccript reduce() Method and spread operatorUsing JavaScript replaceAll()
    4 min read
    How to replace all occurrences of a string using Vue.js filters ?
    In this article, we are going to learn how to replace all occurrences of a particular word with some other word using filters in VueJS. Vue is a progressive framework for building user interfaces. Filters are a functionality provided by Vue components that let you apply formatting and transformation
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences