By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,553 Members | 1,047 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,553 IT Pros & Developers. It's quick & easy.

Replace all words on a page

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
<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

P: n/a
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

P: n/a
<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.