473,775 Members | 2,252 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

show me how to replace all instances of word in textbox

Hi, can someone please show me how to most elegently do this?.....

I have a textbox, and I want to search the contents of it and replace
all instances of a certain word, and replace that word with something
else. For the purposes of this it could be replacing "green" with
"blue". Can someone please show me how to properly do this? :)

Sincerest regards, Alxasa.

Dec 1 '06 #1
17 8134
On 1 Dec 2006 13:41:10 -0800, in comp.lang.javas cript al****@gmail.co m
<11************ *********@j72g2 000cwa.googlegr oups.comwrote:
>| Hi, can someone please show me how to most elegently do this?.....
|
| I have a textbox, and I want to search the contents of it and replace
| all instances of a certain word, and replace that word with something
| else. For the purposes of this it could be replacing "green" with
| "blue". Can someone please show me how to properly do this? :)
|
| Sincerest regards, Alxasa.
<SCRIPT>
re = /apples/gi;
str = "Apples are round, and apples are juicy.";
newstr=str.repl ace(re, "oranges");
document.write( newstr)
</SCRIPT>
---------------------------------------------------------------
jn******@yourpa ntsyahoo.com.au : Remove your pants to reply
---------------------------------------------------------------
Dec 1 '06 #2

Jeff North wrote:
On 1 Dec 2006 13:41:10 -0800, in comp.lang.javas cript al****@gmail.co m
<11************ *********@j72g2 000cwa.googlegr oups.comwrote:
| Hi, can someone please show me how to most elegently do this?.....
|
| I have a textbox, and I want to search the contents of it and replace
| all instances of a certain word, and replace that word with something
| else. For the purposes of this it could be replacing "green" with
| "blue". Can someone please show me how to properly do this? :)
|
| Sincerest regards, Alxasa.

<SCRIPT>
re = /apples/gi;
str = "Apples are round, and apples are juicy.";
newstr=str.repl ace(re, "oranges");
document.write( newstr)
</SCRIPT>
---------------------------------------------------------------
jn******@yourpa ntsyahoo.com.au : Remove your pants to reply
---------------------------------------------------------------\
:) Could you pls show me how to do that same thing to effect a
textarea?

<textarea id=sunny cols=25>Apples are round, and apples are
juicy.</textarea>

<SCRIPT>
re = /apples/gi;
str = "Apples are round, and apples are juicy.";
newstr=str.repl ace(re, "oranges");
document.write( newstr)
</SCRIPT>

Dec 2 '06 #3
On 1 Dec 2006 17:24:21 -0800, in comp.lang.javas cript al****@gmail.co m
<11************ **********@l12g 2000cwl.googleg roups.comwrote:
>| Jeff North wrote:
| On 1 Dec 2006 13:41:10 -0800, in comp.lang.javas cript al****@gmail.co m
| <11************ *********@j72g2 000cwa.googlegr oups.comwrote:
| >
| | Hi, can someone please show me how to most elegently do this?.....
| |
| | I have a textbox, and I want to search the contents of it and replace
| | all instances of a certain word, and replace that word with something
| | else. For the purposes of this it could be replacing "green" with
| | "blue". Can someone please show me how to properly do this? :)
| |
| | Sincerest regards, Alxasa.
| >
| <SCRIPT>
| re = /apples/gi;
| str = "Apples are round, and apples are juicy.";
| newstr=str.repl ace(re, "oranges");
| document.write( newstr)
| </SCRIPT>
|
| :) Could you pls show me how to do that same thing to effect a
| textarea?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitl ed Document</title>
<script type="text/javascript">
function ReplaceApples()
{
//--- setup regex string
re = /apples/gi;
//---- get element
id = document.getEle mentById("sunny ");
//--- get text within element
str = id.value;
//--- do regex replace
newstr=str.repl ace(re, "oranges");
//--- save new value back to element
id.value = newstr;
}
</script>
</head>
<body>
<textarea id="sunny" cols=25>Apples are round, and apples are
juicy.</textarea>
<input name="btn" type="button" onclick="Replac eApples()"
value="Change" />
</body>
</html>
---------------------------------------------------------------
jn******@yourpa ntsyahoo.com.au : Remove your pants to reply
---------------------------------------------------------------
Dec 2 '06 #4

Jeff North wrote:
On 1 Dec 2006 17:24:21 -0800, in comp.lang.javas cript al****@gmail.co m
<11************ **********@l12g 2000cwl.googleg roups.comwrote:
| Jeff North wrote:
| On 1 Dec 2006 13:41:10 -0800, in comp.lang.javas cript al****@gmail.co m
| <11************ *********@j72g2 000cwa.googlegr oups.comwrote:
| >
| | Hi, can someone please show me how to most elegently do this?.....
| |
| | I have a textbox, and I want to search the contents of it and replace
| | all instances of a certain word, and replace that word with something
| | else. For the purposes of this it could be replacing "green" with
| | "blue". Can someone please show me how to properly do this? :)
| |
| | Sincerest regards, Alxasa.
| >
| <SCRIPT>
| re = /apples/gi;
| str = "Apples are round, and apples are juicy.";
| newstr=str.repl ace(re, "oranges");
| document.write( newstr)
| </SCRIPT>
|
| :) Could you pls show me how to do that same thing to effect a
| textarea?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitl ed Document</title>
<script type="text/javascript">
function ReplaceApples()
{
//--- setup regex string
re = /apples/gi;
//---- get element
id = document.getEle mentById("sunny ");
//--- get text within element
str = id.value;
//--- do regex replace
newstr=str.repl ace(re, "oranges");
//--- save new value back to element
id.value = newstr;
}
</script>
</head>
<body>
<textarea id="sunny" cols=25>Apples are round, and apples are
juicy.</textarea>
<input name="btn" type="button" onclick="Replac eApples()"
value="Change" />
</body>
</html>
---------------------------------------------------------------
jn******@yourpa ntsyahoo.com.au : Remove your pants to reply
---------------------------------------------------------------
Very very nice and elegant, Jeff. :) Could you show me how to tweak
your code to account for case-sensitivity for changes?

Dec 2 '06 #5
I am sorry I moved too fast... I now do understand / /gi tags around
apple. If apples was a var reference to a parent frame, like
'parent.somestr ing' instead of just putting a word in there, how would
that work? :) Thank you so much for your assistance. Have a great
day! :)

re = /(parent.somestr ing)/gi ?

Dec 2 '06 #6
wrote on 02 dec 2006 in comp.lang.javas cript:
I am sorry I moved too fast...
[please always quote on usenet]

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Dec 2 '06 #7

Evertjan. wrote:
wrote on 02 dec 2006 in comp.lang.javas cript:
I am sorry I moved too fast...

[please always quote on usenet]

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
I am sorry. Here was the code I am talking about:

<script type="text/javascript">
function ReplaceApples()
{
//--- setup regex string
re = /apples/gi; <--- want to put a top.value="appl es" in
between / /gi
//---- get element
id = document.getEle mentById("sunny ");
//--- get text within element
str = id.value;
//--- do regex replace
newstr=str.repl ace(re, "oranges");
//--- save new value back to element
id.value = newstr;
}

</script>

Dec 2 '06 #8
wrote on 03 dec 2006 in comp.lang.javas cript:
>
Evertjan. wrote:
>wrote on 02 dec 2006 in comp.lang.javas cript:
I am sorry I moved too fast...

[please always quote on usenet]

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

I am sorry. Here was the code I am talking about:

<script type="text/javascript">
function ReplaceApples()
{
//--- setup regex string
re = /apples/gi; <--- want to put a top.value="appl es" in
between / /gi
//---- get element
id = document.getEle mentById("sunny ");
//--- get text within element
str = id.value;
//--- do regex replace
newstr=str.repl ace(re, "oranges");
//--- save new value back to element
id.value = newstr;
}

</script>

var re = new Regex(top.value ,'gi')

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Dec 2 '06 #9
On 2 Dec 2006 14:43:13 -0800, in comp.lang.javas cript al****@gmail.co m
<11************ *********@73g20 00cwn.googlegro ups.comwrote:
>| I am sorry I moved too fast... I now do understand / /gi tags around
| apple. If apples was a var reference to a parent frame, like
| 'parent.somestr ing' instead of just putting a word in there, how would
| that work? :) Thank you so much for your assistance. Have a great
| day! :)
|
| re = /(parent.somestr ing)/gi ?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitl ed Document</title>
<script type="text/javascript">
function ReplaceApples()
{
var id, re, inp, str, newstr, wFrm, wTo;
//--- get text from the input box
wFrm = document.getEle mentById("wrdFr om").value;
wTo = document.getEle mentById("wrdTo ").value;

//--- make new regexp
re = new RegExp(wFrm,"gi ");
id = document.getEle mentById("sunny ");
str = id.value;
newstr=str.repl ace(re, wTo);
id.value = newstr;
}
function ResetChange()
{
document.getEle mentById("sunny ").value="Apple s are round, and apples
are juicy.";
}
</script>
</head>
<body>
<textarea id="sunny" cols=25>Apples are round, and apples are
juicy.</textarea>
<br />
<br />
Word from
<input name="wrdFrom" type="text" id="wrdFrom" value="" />
<br />
Word to
<input name="wrdTo" type="text" id="wrdTo" />
<br />
<input name="btn" type="button" onclick="Replac eApples()"
value="Change" />
<input type="button" name="Button" value="Reset"
onclick="ResetC hange()"/>
</body>
</html>
---------------------------------------------------------------
jn******@yourpa ntsyahoo.com.au : Remove your pants to reply
---------------------------------------------------------------
Dec 2 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
4658
by: oLE | last post by:
I would like to add some javascript to show/hide a certain row of a table. The first row of the table contain the hyperlink that calls the javascript the second row is the one i want to show/hide with the javascript in a toggle fashion. the problem is a know very little javascript and have become incredibly frustrated because i went ahead thinking it was going to be like C. its not. I know i can use these lines to do the actual work:
16
12398
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" >
3
8896
by: gregpinero | last post by:
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="""
1
1967
by: Tomomichi Amano | last post by:
Could some one tell me how I can seach and replace only one word in a textBox (THE FIRST WORD THAT COMES AFTER THE CURSOR). I already know how to replace ALL , but I don't know how to REPLACE one, and how to SEARCH one and select that point. Thank in advance
1
1680
by: Tomomichi Amano | last post by:
Hello. I want to make replace & search functions in my text editor. Thanks to the kind people here at the newsgroup, I was able to make the function. But I was not able to understand how to REPLACE the next word (the nearest word from the cursor; not REPLACE ALL, but replace only one word) and SEARCH the next word. COuld some one help me? Thanks in advance.
16
38201
by: Ramsin Savra | last post by:
Hi, What do you suggest to do if I want to do a search in TextBox ? I know that using RichTextBox, we could have Find method to search for a specific word or information but I was wonder if somebody knows some methods for Find and Replace in TextBox. Thanks
20
2449
by: ed | last post by:
Hi I can't seem to make the Me.Show() work My code is dim dlgresult as dialogresul dlgresult=me.show( The message is : "expression does not produce a value
7
21018
by: gar | last post by:
Hi, I need to replace all the double quotes (") in a textbox with single quotes ('). I used this code text= Replace(text, """", "'" This works fine (for normal double quotes).The problem comes in when you copy a double quote from MS Word and paste it in the text box. What happens is the double quote becomes slanted (“) so my code above can't filter it. I tried to do this text= Replace(text, "““","'")
3
2489
by: gregpinero | last post by:
Hi guys, What I'm trying to do is find all instances of an acronymn such as IBM on a webpage and replace it with <acronym title="International Business Machines">IBM</acronym>. However in my code below it replace the <, and > with &lt; and &gt;. Thus it replaces IBM with: &lt;acronym title="International Business Machines"&gt;IBM&lt;/acronym&gt;
0
9622
marktang
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...
0
9454
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,...
0
10270
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10051
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,...
0
9916
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8940
agi2029
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...
1
7464
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6718
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();...
0
5486
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.