473,434 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,434 software developers and data experts.

Replace all words on a page

I'm trying to write a little script that will have a list of word pairs
which will loop through that list and replace all instances of each
word with the other word.

I'm very new to javascript so I'm not quite sure where to get started.
Here is my python "prototype" in case you want a better idea of what
I'm trying to do:

<code>
corpus="""
We always love war. Run away all of you he ordered.
""" #Just some random text I made up. This would be the html in the
javascript version

findreplacepairs=[
("war","peace"),
("run","walk"),
("order","request")
]

for item in findreplacepairs:
\t corpus.replace(item[0],item[1]) #replaces all instances

</code>

Thanks,

Greg

Aug 10 '05 #1
3 8864
<gr********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
I'm trying to write a little script that will have a list of word pairs
which will loop through that list and replace all instances of each
word with the other word.

I'm very new to javascript so I'm not quite sure where to get started.
Here is my python "prototype" in case you want a better idea of what
I'm trying to do:

<code>
corpus="""
We always love war. Run away all of you he ordered.
""" #Just some random text I made up. This would be the html in the
javascript version

findreplacepairs=[
("war","peace"),
("run","walk"),
("order","request")
]

for item in findreplacepairs:
\t corpus.replace(item[0],item[1]) #replaces all instances

</code>

Thanks,

Greg


Will this get you started? Watch for word-wrap.

It ignores capitalization and replaces embedded words.

Before:
We always love war. Run away all of you he ordered.
After:
We always love peace. walk away all of you he requested.
<script type="text/javascript">
var corpus = "We always love war. Run away all of you he ordered."
var pairs = [
"war=peace",
"run=walk",
"order=request"];
var temper = corpus;
for (var i=0; i<pairs.length; i++) {
var words = pairs[i].split("=");
var regex = new RegExp(words[0],"ig");
temper = temper.replace(regex,words[1]);
}
alert(corpus + "\n" + temper);
</script>
Aug 10 '05 #2
very neat! Thanks! My main remaining question is how do I get at all
the text from a page? I need some sort of dom thingy from what I've
read.
A few other questions below:

<script type="text/javascript">
var corpus = "We always love war. Run away all of you he ordered."
var pairs = [
"war=peace",
"run=walk",
"order=request"];
var temper = corpus;
for (var i=0; i<pairs.length; i++) {
var words = pairs[i].split("=");
var regex = new RegExp(words[0],"ig");
[GP] What does above line do?
temper = temper.replace(regex,words[1]);
[GP] in above line, how do you say replace all instances vs replace
first?
}

alert(corpus + "\n" + temper);
</script>

Thanks again,

Greg

Aug 10 '05 #3
<gr********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
very neat! Thanks! My main remaining question is how do I get at all
the text from a page? I need some sort of dom thingy from what I've
read.
A few other questions below:

<script type="text/javascript">
var corpus = "We always love war. Run away all of you he ordered."
var pairs = [
"war=peace",
"run=walk",
"order=request"];
var temper = corpus;
for (var i=0; i<pairs.length; i++) {
var words = pairs[i].split("=");
var regex = new RegExp(words[0],"ig");
[GP] What does above line do?
temper = temper.replace(regex,words[1]);
[GP] in above line, how do you say replace all instances vs replace
first?
}

alert(corpus + "\n" + temper);
</script>

Thanks again,

Greg


"how do I get at all the text from a page?"
you can use XMLHTTP to retrieve a Web page;
is that what you mean by "from a page"?
var regex = new RegExp(words[0],"ig");
[GP] What does above line do?

Constructs a regular expression of the "replace with" word'
"ig" specifies ignore case and global (all instances).

temper = temper.replace(regex,words[1]);
[GP] in above line, how do you say replace all instances vs replace first?

The "g" in the "ig" says replace all instances.
Remove it if you don't want to.
Aug 10 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Leif K-Brooks | last post by:
For my web-based application, I need to make twelve different calls to str.replace for the content of /every/ page on /every/ page view (or find a harder to implement and probably less elegant...
4
by: Jane Doe | last post by:
Hi, I need to search and replace patterns in web pages, but I can't find a way even after reading the ad hoc chapter in New Rider's "Inside JavaScript". Here's what I want to do: function...
3
by: Andy | last post by:
Can someone please help me with this problem? I don't know if this is possible in Java, so if it's not please point me in the right direction or to the right newsgroup! (I'm a newby to Java...
4
by: Prasad S | last post by:
Hello I wish to replace all the characters in a string except those which are inside '<' & '>' characters. And there could be multiple occurences of < & > within the string. e.g. string =...
16
by: juglesh | last post by:
Hello, I need to look through the text on a page and replace certain words with an image or other word something like: read document find all instances of the word "blah" change all...
4
by: lucky | last post by:
hi there!! i'm looking for a code snipett wich help me to search some words into a particular string and replace with a perticular word. i got a huge data string in which searching traditional...
1
by: Curious | last post by:
I'm working on a word replacement program in .NET. I have a list of words spelt in American English and need to replace them with translated British spelling while keeping the upper or lower cases...
2
by: rjoseph | last post by:
Hi Guys I hope this is a simple one for you. I am basically displaying data onto my xml page using the following line of code: <xsl:value-of select="carmanufacturer" /> An example of the...
8
by: Avi | last post by:
Hi all, I'm using string Replace(string oldValue, string newValue) and would like it to replace only full words that matches oldValue and not when oldValue is a substring of a larger word. ...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.