Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-27-2012, 10:57 PM   PM User | #1
webshark
New to the CF scene

 
Join Date: Sep 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
webshark is an unknown quantity at this point
Find and replace multiple words with javascript

I am looking for a way to find and replace multiple words within a sentence using javascript. Ideally I would like to take a sentence and replace multiple words within that sentence i.e.

text **WORD 1** text | text **WORD 2** text

How can I replace both WORD1 and WORD2 with independent different words?

i.e.
WORD1= New-word-1
WORD2= New-word-2

here is the code:
http://jsfiddle.net/yaaf8/

and to take it one step further I would like to pull new word values from an excel sheet, run a loop with the new words to be replaced and have the output of the new sentences all on one page.

Any guidance would be greatly appreciated.
webshark is offline   Reply With Quote
Old 09-27-2012, 11:11 PM   PM User | #2
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
possibly missing the point here, but...
Code:
var visitorName = "new word 1";
var loc = "here";
var myOldString = "Hello word1! I hope you enjoy your stay word2.";
var myNewString = myOldString.replace(/word1/g, visitorName).replace(/word2/g, loc);
?
xelawho is offline   Reply With Quote
Old 09-28-2012, 08:03 AM   PM User | #3
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
This perhaps?

Code:
<div id = "mydiv">Now is the time for all good men to come to the aid of their country.</div>
<div id = "mydiv1"></div>

<script type = "text/javascript">

var ow = ["now", "time","good","men","country"];
var nw = ["tomorrow", "occasion", "fine", "women", "state"];

var txt = document.getElementById("mydiv").innerHTML;
var txtsplit = txt.split(" ");

for (var i =0; i<txtsplit.length; i++) {
for (var j = 0; j <ow.length; j++) {
if (txtsplit[i].toLowerCase() == ow[j]) {
txtsplit[i] = nw[j];
}
}
}

var newtext = txtsplit.join(" ");
newtext = newtext.charAt(0).toUpperCase() + newtext.slice(1); // capitalise first letter of string
document.getElementById("mydiv1").innerHTML = newtext;

</script>

For importing Excel sheet in Javascript see

http://www.roseindia.net/javascript/...ile-data.shtml

But it would be simpler to use a text file. Or just have the words in a Javascript array (as above).




The whole aim of practical politics is to keep the populace alarmed (and hence clamorous to be led to safety)by menacing it with an endless series of hobgoblins, all of them imaginary. - H.L. Mencken 1880-1956, American Editor, Author, Critic, Humorist
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 09-28-2012 at 08:15 AM..
Philip M is offline   Reply With Quote
Reply

Bookmarks

Tags
javascript, jquery, programming

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:11 AM.


Advertisement
Log in to turn off these ads.