![]() |
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. |
Re: show me how to replace all instances of word in textbox
On 1 Dec 2006 13:41:10 -0800, in comp.lang.javascript alxasa@gmail.com
<1165009270.429162.34030@j72g2000cwa.googlegroups. com> wrote: >| 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> --------------------------------------------------------------- jnorthau@yourpantsyahoo.com.au : Remove your pants to reply --------------------------------------------------------------- |
Re: show me how to replace all instances of word in textbox
Jeff North wrote: > On 1 Dec 2006 13:41:10 -0800, in comp.lang.javascript alxasa@gmail.com > <1165009270.429162.34030@j72g2000cwa.googlegroups. com> wrote: > > >| 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> > --------------------------------------------------------------- > jnorthau@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> |
Re: show me how to replace all instances of word in textbox
On 1 Dec 2006 17:24:21 -0800, in comp.lang.javascript alxasa@gmail.com
<1165022661.290025.122520@l12g2000cwl.googlegroups .com> wrote: >| Jeff North wrote: >| > On 1 Dec 2006 13:41:10 -0800, in comp.lang.javascript alxasa@gmail.com >| > <1165009270.429162.34030@j72g2000cwa.googlegroups. com> wrote: >| > >| > >| 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> --------------------------------------------------------------- jnorthau@yourpantsyahoo.com.au : Remove your pants to reply --------------------------------------------------------------- |
Re: show me how to replace all instances of word in textbox
Jeff North wrote: > On 1 Dec 2006 17:24:21 -0800, in comp.lang.javascript alxasa@gmail.com > <1165022661.290025.122520@l12g2000cwl.googlegroups .com> wrote: > > >| Jeff North wrote: > >| > On 1 Dec 2006 13:41:10 -0800, in comp.lang.javascript alxasa@gmail.com > >| > <1165009270.429162.34030@j72g2000cwa.googlegroups. com> wrote: > >| > > >| > >| 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> > --------------------------------------------------------------- > jnorthau@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? |
Re: show me how to replace all instances of word in textbox
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: show me how to replace all instances of word in textbox
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) |
Re: show me how to replace all instances of word in textbox
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> |
Re: show me how to replace all instances of word in textbox
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) |
Re: show me how to replace all instances of word in textbox
On 2 Dec 2006 14:43:13 -0800, in comp.lang.javascript alxasa@gmail.com
<1165099393.504138.62500@73g2000cwn.googlegroups.c om> 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 ? <!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> --------------------------------------------------------------- jnorthau@yourpantsyahoo.com.au : Remove your pants to reply --------------------------------------------------------------- |
| All times are GMT. The time now is 02:58 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.