kareem_elzeiny Posted November 15, 2011 Share Posted November 15, 2011 Hi, using this method how can I replace more than one word ? var str="Visit Microsoft!";document.write(str.replace(/microsoft/gi, "W3Schools")); Regards, Link to comment Share on other sites More sharing options...
artistandlive Posted November 15, 2011 Share Posted November 15, 2011 What do you want to do?1.) Replace all occurrences of "microsoft"? ... What you are doing already with /g2.) Replace more than one word with w3Schools?3.) Replace more multiple words with multiple words? Link to comment Share on other sites More sharing options...
kareem_elzeiny Posted November 15, 2011 Author Share Posted November 15, 2011 1) /g is to Perform a global search (the word Microsoft will be replaced each time it is found): the phrase above is not the exact in the script it's just an example I meant > 3.) Replace more multiple words with multiple words Link to comment Share on other sites More sharing options...
artistandlive Posted November 15, 2011 Share Posted November 15, 2011 This won't work with the replace function (as far as I know).You can only replace your pattern with one string, so you have to wrap it in a loop (e.g. a foreach loop with a string array) Link to comment Share on other sites More sharing options...
kareem_elzeiny Posted November 15, 2011 Author Share Posted November 15, 2011 thanks for help, but I don't know anything about looping.can you illustrate more if there is a tutorial page or an example ? Link to comment Share on other sites More sharing options...
ShadowMage Posted November 15, 2011 Share Posted November 15, 2011 You'll have to have an array of the strings you're searching for, and an array of strings you're replacing them with. Then you can loop through each string you're looking for and perform the replacement. The tutorial page for looping is here. You'll also see a page for while loops, but the for loop is better suited for this task. Link to comment Share on other sites More sharing options...
kareem_elzeiny Posted November 17, 2011 Author Share Posted November 17, 2011 Thanks for your help, I've tried but I couldn't do it !! Link to comment Share on other sites More sharing options...
Synook Posted November 17, 2011 Share Posted November 17, 2011 What is the result of your attempts? Link to comment Share on other sites More sharing options...
ShadowMage Posted November 17, 2011 Share Posted November 17, 2011 Yes, please post the code you tried so we can take a look at it. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.