473,396 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 8073
On 1 Dec 2006 13:41:10 -0800, in comp.lang.javascript al****@gmail.com
<11*********************@j72g2000cwa.googlegroups. 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.replace(re, "oranges");
document.write(newstr)
</SCRIPT>
---------------------------------------------------------------
jn******@yourpantsyahoo.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.javascript al****@gmail.com
<11*********************@j72g2000cwa.googlegroups. 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.replace(re, "oranges");
document.write(newstr)
</SCRIPT>
---------------------------------------------------------------
jn******@yourpantsyahoo.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.replace(re, "oranges");
document.write(newstr)
</SCRIPT>

Dec 2 '06 #3
On 1 Dec 2006 17:24:21 -0800, in comp.lang.javascript al****@gmail.com
<11**********************@l12g2000cwl.googlegroups .comwrote:
>| Jeff North wrote:
| On 1 Dec 2006 13:41:10 -0800, in comp.lang.javascript al****@gmail.com
| <11*********************@j72g2000cwa.googlegroups. 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.replace(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>Untitled Document</title>
<script type="text/javascript">
function ReplaceApples()
{
//--- setup regex string
re = /apples/gi;
//---- get element
id = document.getElementById("sunny");
//--- get text within element
str = id.value;
//--- do regex replace
newstr=str.replace(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="ReplaceApples()"
value="Change" />
</body>
</html>
---------------------------------------------------------------
jn******@yourpantsyahoo.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.javascript al****@gmail.com
<11**********************@l12g2000cwl.googlegroups .comwrote:
| Jeff North wrote:
| On 1 Dec 2006 13:41:10 -0800, in comp.lang.javascript al****@gmail.com
| <11*********************@j72g2000cwa.googlegroups. 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.replace(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>Untitled Document</title>
<script type="text/javascript">
function ReplaceApples()
{
//--- setup regex string
re = /apples/gi;
//---- get element
id = document.getElementById("sunny");
//--- get text within element
str = id.value;
//--- do regex replace
newstr=str.replace(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="ReplaceApples()"
value="Change" />
</body>
</html>
---------------------------------------------------------------
jn******@yourpantsyahoo.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.somestring' 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.somestring)/gi ?

Dec 2 '06 #6
wrote on 02 dec 2006 in comp.lang.javascript:
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.javascript:
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="apples" in
between / /gi
//---- get element
id = document.getElementById("sunny");
//--- get text within element
str = id.value;
//--- do regex replace
newstr=str.replace(re, "oranges");
//--- save new value back to element
id.value = newstr;
}

</script>

Dec 2 '06 #8
wrote on 03 dec 2006 in comp.lang.javascript:
>
Evertjan. wrote:
>wrote on 02 dec 2006 in comp.lang.javascript:
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="apples" in
between / /gi
//---- get element
id = document.getElementById("sunny");
//--- get text within element
str = id.value;
//--- do regex replace
newstr=str.replace(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.javascript al****@gmail.com
<11*********************@73g2000cwn.googlegroups.c omwrote:
>| 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.somestring' 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.somestring)/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>Untitled Document</title>
<script type="text/javascript">
function ReplaceApples()
{
var id, re, inp, str, newstr, wFrm, wTo;
//--- get text from the input box
wFrm = document.getElementById("wrdFrom").value;
wTo = document.getElementById("wrdTo").value;

//--- make new regexp
re = new RegExp(wFrm,"gi");
id = document.getElementById("sunny");
str = id.value;
newstr=str.replace(re, wTo);
id.value = newstr;
}
function ResetChange()
{
document.getElementById("sunny").value="Apples 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="ReplaceApples()"
value="Change" />
<input type="button" name="Button" value="Reset"
onclick="ResetChange()"/>
</body>
</html>
---------------------------------------------------------------
jn******@yourpantsyahoo.com.au : Remove your pants to reply
---------------------------------------------------------------
Dec 2 '06 #10

Jeff North wrote:
On 2 Dec 2006 14:43:13 -0800, in comp.lang.javascript al****@gmail.com
<11*********************@73g2000cwn.googlegroups.c omwrote:
| 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.somestring' 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.somestring)/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>Untitled Document</title>
<script type="text/javascript">
function ReplaceApples()
{
var id, re, inp, str, newstr, wFrm, wTo;
//--- get text from the input box
wFrm = document.getElementById("wrdFrom").value;
wTo = document.getElementById("wrdTo").value;

//--- make new regexp
re = new RegExp(wFrm,"gi");
id = document.getElementById("sunny");
str = id.value;
newstr=str.replace(re, wTo);
id.value = newstr;
}
function ResetChange()
{
document.getElementById("sunny").value="Apples 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="ReplaceApples()"
value="Change" />
<input type="button" name="Button" value="Reset"
onclick="ResetChange()"/>
</body>
</html>
---------------------------------------------------------------
jn******@yourpantsyahoo.com.au : Remove your pants to reply
---------------------------------------------------------------
Thank you Jeff, and all so much :) Have a nice day.

Dec 3 '06 #11
al****@gmail.com wrote:
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.somestring' 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.somestring)/gi ?
re = new RegEx(parent.somestring,"gi")

or

re= eval("/"+parent.somestring+"/gi")

Mick
Dec 3 '06 #12
mick white wrote:
al****@gmail.com wrote:
>re = /(parent.somestring)/gi ?
re = new RegEx(parent.somestring,"gi")
OK.
or

re= eval("/"+parent.somestring+"/gi")
Nonsense.
PointedEars
Dec 8 '06 #13
Thomas 'PointedEars' Lahn wrote:
mick white wrote:
> re = new RegEx(parent.somestring,"gi")


OK.

>>or

re= eval("/"+parent.somestring+"/gi")


Nonsense.
Not the best, perhaps, but "Nonsense"? No.
Mick

Dec 9 '06 #14
mick white wrote on 09 dec 2006 in comp.lang.javascript:
Thomas 'PointedEars' Lahn wrote:
>mick white wrote:
>> re = new RegEx(parent.somestring,"gi")
OK.
>>>or
re= eval("/"+parent.somestring+"/gi")

Nonsense.

Not the best, perhaps, but "Nonsense"? No.
Good of you to wash some ears, Mick, but eval() is evil.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Dec 9 '06 #15
mick white wrote:
Thomas 'PointedEars' Lahn wrote:
>mick white wrote:
>> re = new RegEx(parent.somestring,"gi")
^
>>
OK.
That was premature. The constructor is RegExp().
>>or

re= eval("/"+parent.somestring+"/gi")


Nonsense.

Not the best, perhaps, but "Nonsense"? No.
If you do not consider it nonsense, then you have to deal with this
question:

What does this accomplish more that cannot be done better with calling the
constructor function?
PointedEars
--
Indiana Jones: The Name of God. Jehovah.
Professor Henry Jones: But in the Latin alphabet,
"Jehovah" begins with an "I".
Indiana Jones: J-...
Dec 9 '06 #16
Thomas 'PointedEars' Lahn wrote:
mick white wrote:

>>Thomas 'PointedEars' Lahn wrote:
>>>mick white wrote:

re = new RegEx(parent.somestring,"gi")

^
>>>OK.


That was premature. The constructor is RegExp().
Ahh, the missing "p", good catch.
>
>>>>or

re= eval("/"+parent.somestring+"/gi")
Nonsense.

Not the best, perhaps, but "Nonsense"? No.


If you do not consider it nonsense, then you have to deal with this
question:

What does this accomplish more that cannot be done better with calling the
constructor function?
It handles control characters, "\n", "\t" etc..., a little better.
Mick
>

PointedEars
Dec 9 '06 #17
mick white wrote:
Thomas 'PointedEars' Lahn wrote:
>mick white wrote:
>>>Thomas 'PointedEars' Lahn wrote:
mick white wrote:
>re= eval("/"+parent.somestring+"/gi")
Nonsense.
Not the best, perhaps, but "Nonsense"? No.

If you do not consider it nonsense, then you have to deal with this
question:

What does this accomplish more that cannot be done better with calling
the constructor function?

It handles control characters, "\n", "\t" etc..., a little better.
In what way, please?
PointedEars
--
The English government is much of a German poodle as
other governments. The Germans infiltrated them all.
-- "The only real Barbara Schwarz", dsw.scientology,
<16**************************@posting.google.com >)
Dec 9 '06 #18

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

Similar topics

10
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...
16
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...
3
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...
1
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,...
1
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...
16
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...
20
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
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...
3
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
jinu1996
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...
0
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...
0
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...
0
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,...

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.