Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to replace multiple strings with another multiple strings

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 186
    Comment on it

    Hello Reader's! If you want to replace multiple sting with another multiple stings, then you use the JS function as below:-

    Lets say a given sentence having strings.

    var str = "I have a cat, a dog, and a goat."; //sentence to process
    var mapObj = {
       cat:"dog", //specify what word will replace with another words.
       dog:"goat",
       goat:"cat"
    
    };
    str = str.replace(/cat|dog|goat/gi, function(matched){
      return mapObj[matched];
    });
    
    alert(str);
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: