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

show me how to replace all instances of word in textbox

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


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

P: n/a

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

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

P: n/a

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

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

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

P: n/a

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

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

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

P: n/a

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

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

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

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

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

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

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.