Replace all words on a page

G

gregpinero

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
 
M

McKirahan

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.split("=");
var regex = new RegExp(words[0],"ig");
temper = temper.replace(regex,words[1]);
}
alert(corpus + "\n" + temper);
</script>
 
G

gregpinero

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.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
 
M

McKirahan

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.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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top