Replace multiple strings with multiple other strings in JavaScript Last Updated : 30 May, 2024 Comments Improve Suggest changes Like Article Like Report In this article, we are given a Sentence having multiple strings. The task is to replace multiple strings with new strings simultaneously instead of doing it one by one, using JavaScript. Below are a few methods to understand: Table of Content Using JavaScript replace() method Using the JavaScript str.replaceAll() methodUsing Array.reduce() method:Using JavaScript replace() method This method searches a string for a defined value, or a regular expression, and returns a new string with the replaced defined value. Syntax: string.replace(searchVal, newvalue);Example: This example uses the RegExp to replace the strings according to the object using the replace() method. JavaScript let str = "I have a Lenovo Laptop, a Honor Phone, and a Samsung Tab."; let Obj = { Lenovo: "Dell", Honor: "OnePlus", Samsung: "Lenovo" }; function GFG_Fun() { console.log(str.replace(/Lenovo|Honor|Samsung/gi, function (matched) { return Obj[matched]; })); } GFG_Fun() OutputI have a Dell Laptop, a OnePlus Phone, and a Lenovo Tab. Using the JavaScript str.replaceAll() methodIn this example, we will see the use of the JavaScript str.replaceAll() method for replacing multiple strings. Example: This example shows the implementation of the above-explained appraoch. JavaScript const str = 'who.where_when-how'; const result = str .replaceAll('.', '?') .replaceAll('_', '?') .replaceAll('-', '?'); console.log(result); Outputwho?where?when?how Using Array.reduce() method:Using Array.reduce() with replaceAll() method, the approach iterates over an array of replacement pairs, applying each replacement to the string sequentially. It uses regular expressions to globally replace occurrences of each old string with its corresponding new string. Example: In this example we replaces multiple characters in a string with '?' using Array.reduce() and replaceAll(). JavaScript const str = 'who.where_when-how'; const replacements = [ ['.', '?'], ['_', '?'], ['-', '?'] ]; const result = replacements.reduce((acc, [oldStr, newStr]) => { return acc.replaceAll(oldStr, newStr); }, str); console.log(result); Outputwho?where?when?how Using JavaScript String.split() and Array.join() MethodThis method involves using split() to break the string at each target substring and then using join() to reassemble the string with the new substring in place of the target substrings. Example: In this example, we will replace multiple substrings using split() and join() methods. JavaScript let str = "I have a Lenovo Laptop, a Honor Phone, and a Samsung Tab."; let replacements = { "Lenovo": "Dell", "Honor": "OnePlus", "Samsung": "Lenovo" }; function replaceMultiple(str, replacements) { for (let [oldStr, newStr] of Object.entries(replacements)) { str = str.split(oldStr).join(newStr); } return str; } console.log(replaceMultiple(str, replacements)); OutputI have a Dell Laptop, a OnePlus Phone, and a Lenovo Tab. Comment More infoAdvertise with us Next Article Replace multiple strings with multiple other strings in JavaScript P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies javascript-string JavaScript-DSA JavaScript-Questions +1 More Similar Reads Create a string with multiple spaces in JavaScript We have a string with extra spaces and if we want to display it in the browser then extra spaces will not be displayed. Adding the number of spaces to the string can be done in the following ways. In this article, we are going to learn how to Create a string with multiple spaces in JavaScript. Below 3 min read Replace Multiple Words with K in JavaScript Sometimes, while working with Javascript strings, we may face a problem in which we have to perform a replacement of multiple words with a single word. This can have applications in many domains like day-day programming and school programming. These are the following approaches:Table of Content Usin 5 min read JavaScript - How to Replace Multiple Spaces with a Single Space? Here are the various methods to replace multiple spaces with a single space in JavaScript.1. Using replace() Method with Regular Expression (Most Common)The replace() method combined with a regular expression is the most efficient way to replace multiple spaces with a single space.JavaScriptconst s1 2 min read JavaScript - How to Match Multiple Parts of a String Using RegExp? In JavaScript, the regular expression (RegExp) object provides a powerful mechanism to search, match, and manipulate strings. One of the common use cases is matching multiple parts of a string that satisfy a certain pattern. You can easily do this using global (g) and other RegExp features.1. Using 3 min read How to replace plain URL with link using JavaScript ? Given a plane URL, the task is to replace the plain URLs with the links. This problem can be solved with the help of Regular Expressions. Approach:Â Using RegExp and replace() MethodRegExp - This looks for the URL in the provided text.This RegExp parses the URL and puts the address to the $1 variable 2 min read 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 How to replace the names of multiple object keys with the values provided using JavaScript ? The following approach covers how to replace the names of multiple object keys with the values provided by the user using JavaScript. Problem Statement: You are given an object which contains different key-value pairs in which key symbolizes the property and value itself is known as the property val 4 min read How to replace a portion of strings with another value in JavaScript ? In JavaScript, replacing a portion of strings with another value involves using the `replace()` method or regular expressions. This process is essential for text manipulation tasks like formatting, sanitization, or dynamic content generation in web development and data processing applications. We ca 3 min read JavaScript string replace() Method JavaScript replace() method is used for manipulating strings. It allows you to search for a specific part of a string, called a substring, and then replace it with another substring. What's great is that this method doesn't alter the original string, making it ideal for tasks where you want to maint 5 min read How to replace multiple characters in a string in PHP ? A string is a sequence of characters enclosed within single or double quotes. A string can also be looped through and modifications can be made to replace a particular sequence of characters in it. In this article, we will see how to replace multiple characters in a string in PHP.Using the str_repla 3 min read Like