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
findreplacepair s=[
("war","peace") ,
("run","walk "),
("order","reque st")
]
for item in findreplacepair s:
\t corpus.replace( item[0],item[1]) #replaces all instances
</code>
Thanks,
Greg 3 8894
<gr********@gma il.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.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
findreplacepair s=[ ("war","peace") , ("run","walk "), ("order","reque st") ]
for item in findreplacepair s: \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=requ est"];
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>
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=requ est"];
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
<gr********@gma il.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.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=requ est"]; 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 solution). Would
that be a major performance hit?
|
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 filter() {
var items = new Array("John", "Jane");
|
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 scripting).
On a web page I have a webcam type image that updates every minute. I
wish to conditionally replace this image with an apology image in the
event that my upload fails for more than an hour. I guess what I want is
some script that will...
|
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 = "this is an example of <how> many words could be hidden
<under> these characters"
now, from this string all the characters should be searched & replaced
|
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 instances of the word "blah" to <img src="MyPicture.jpg" >
| |
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 way mean to secrife lots of time
in asp.net. can any one give me such a expression in which i pass a
data string and search word string and replace word string? if so plz
help me out. i'm in badly need.
|
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 of
each letter in these words.
For instance, I have a dictionary below:
theater theatre
center centre
annualized annualised
|
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 resulting output would be, "Ford"
|
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.
How can it be done?
Thanks,
Avi
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |