469,336 Members | 5,156 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,336 developers. It's quick & easy.

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 8600
<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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Leif K-Brooks | last post: by
4 posts views Thread by Jane Doe | last post: by
3 posts views Thread by Andy | last post: by
2 posts views Thread by rjoseph | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by haryvincent176 | last post: by
reply views Thread by Marylou17 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.