473,215 Members | 1,242 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,215 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 8846
<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
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.