Skip to content
Related Articles
Open in App
Not now

Related Articles

Replace multiple strings with multiple other strings in JavaScript

Improve Article
Save Article
  • Last Updated : 05 Jan, 2023
Improve Article
Save Article

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:

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. 


string.replace(searchVal, newvalue)

Example: This example uses the RegExp to replace the strings according to the object using the replace() method.


<body style="text-align:center;" id="body">
    <h1 style="color:green;">GeeksForGeeks</h1>
        JavaScript | Replace multiple strings with multiple other strings.
    <p id="GFG_UP" style="font-size: 19px; font-weight: bold;">
    <button onClick="GFG_Fun()">
        click here
    <p id="GFG_DOWN" style="color: green; font-size: 24px; font-weight: bold;">
        var up = document.getElementById('GFG_UP');
        var down = document.getElementById('GFG_DOWN');
        var str = "I have a Lenovo Laptop, a Honor Phone, and a Samsung Tab.";
        var Obj = {
            Lenovo: "Dell",
            Honor: "OnePlus",
            Samsung: "Lenovo"
        up.innerHTML = str;
        function GFG_Fun() {
            down.innerHTML = str.replace(/Lenovo|Honor|Samsung/gi, function(matched){
                return Obj[matched];


Replace multiple strings with multiple other strings

Replace multiple strings with multiple other strings

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!