Replace multiple strings with multiple other strings in JavaScript
Last Updated : 30 May, 2024
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:
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() method
In 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);
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);
Using JavaScript String.split() and Array.join() Method
This 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.
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
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 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