show me how to replace all instances of word in textbox

A

alxasa

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

Jeff North

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

alxasa

Jeff said:
<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?

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

Jeff North

| Jeff North wrote:
| > On 1 Dec 2006 13:41:10 -0800, in comp.lang.javascript (e-mail address removed)
| >
| > >| 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>
 
A

alxasa

Jeff said:
<!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>

Very very nice and elegant, Jeff. :) Could you show me how to tweak
your code to account for case-sensitivity for changes?
 
A

alxasa

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 ?
 
A

alxasa

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

[please always quote on usenet]

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

Evertjan.

wrote on 03 dec 2006 in comp.lang.javascript:
Evertjan. said:
wrote on 02 dec 2006 in comp.lang.javascript:
I am sorry I moved too fast...

[please always quote on usenet]

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')
 
J

Jeff North

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

alxasa

Jeff said:
<!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>

Thank you Jeff, and all so much :) Have a nice day.
 
M

mick white

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
 
E

Evertjan.

mick white wrote on 09 dec 2006 in comp.lang.javascript:
Not the best, perhaps, but "Nonsense"? No.

Good of you to wash some ears, Mick, but eval() is evil.
 
T

Thomas 'PointedEars' Lahn

That was premature. The constructor is RegExp().
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
 
M

mick white

Thomas said:
mick white wrote:




That was premature. The constructor is RegExp().

Ahh, the missing "p", good catch.
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
 

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,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top