show me how to replace all instances of word in textbox

Discussion in 'Javascript' started by alxasa@gmail.com, Dec 1, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. Jeff North Guest

    On 1 Dec 2006 13:41:10 -0800, in comp.lang.javascript
    <> 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>
    ---------------------------------------------------------------
    : Remove your pants to reply
    ---------------------------------------------------------------
     
    Jeff North, Dec 1, 2006
    #2
    1. Advertising

  3. Guest

    Jeff North wrote:
    > On 1 Dec 2006 13:41:10 -0800, in comp.lang.javascript
    > <> 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>
    > ---------------------------------------------------------------
    > : 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, 2006
    #3
  4. Jeff North Guest

    On 1 Dec 2006 17:24:21 -0800, in comp.lang.javascript
    <> wrote:

    >| Jeff North wrote:
    >| > On 1 Dec 2006 13:41:10 -0800, in comp.lang.javascript
    >| > <> 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>
    ---------------------------------------------------------------
    : Remove your pants to reply
    ---------------------------------------------------------------
     
    Jeff North, Dec 2, 2006
    #4
  5. Guest

    Jeff North wrote:
    > On 1 Dec 2006 17:24:21 -0800, in comp.lang.javascript
    > <> wrote:
    >
    > >| Jeff North wrote:
    > >| > On 1 Dec 2006 13:41:10 -0800, in comp.lang.javascript
    > >| > <> 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>
    > ---------------------------------------------------------------
    > : 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, 2006
    #5
  6. Guest

    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, 2006
    #6
  7. Evertjan. Guest

    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)
     
    Evertjan., Dec 2, 2006
    #7
  8. Guest

    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, 2006
    #8
  9. Evertjan. Guest

    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)
     
    Evertjan., Dec 2, 2006
    #9
  10. Jeff North Guest

    On 2 Dec 2006 14:43:13 -0800, in comp.lang.javascript
    <> 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>
    ---------------------------------------------------------------
    : Remove your pants to reply
    ---------------------------------------------------------------
     
    Jeff North, Dec 3, 2006
    #10
  11. Guest

    Jeff North wrote:
    > On 2 Dec 2006 14:43:13 -0800, in comp.lang.javascript
    > <> 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>
    > ---------------------------------------------------------------
    > : Remove your pants to reply
    > ---------------------------------------------------------------


    Thank you Jeff, and all so much :) Have a nice day.
     
    , Dec 3, 2006
    #11
  12. mick white Guest

    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
     
    mick white, Dec 3, 2006
    #12
  13. mick white wrote:

    > wrote:
    >> re = /(parent.somestring)/gi ?
    >>

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


    OK.

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


    Nonsense.


    PointedEars
     
    Thomas 'PointedEars' Lahn, Dec 8, 2006
    #13
  14. mick white Guest

    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
     
    mick white, Dec 9, 2006
    #14
  15. Evertjan. Guest

    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)
     
    Evertjan., Dec 9, 2006
    #15
  16. 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-...
     
    Thomas 'PointedEars' Lahn, Dec 9, 2006
    #16
  17. mick white Guest

    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
     
    mick white, Dec 9, 2006
    #17
  18. 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,
    <>)
     
    Thomas 'PointedEars' Lahn, Dec 9, 2006
    #18
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    583
    Evertjan.
    Sep 30, 2005
  2. Tom Cross
    Replies:
    1
    Views:
    337
    Terry Reedy
    Aug 16, 2003
  3. John Wohlbier
    Replies:
    2
    Views:
    368
    Josiah Carlson
    Feb 22, 2004
  4. Replies:
    8
    Views:
    468
    James Stroud
    Jan 29, 2009
  5. Replies:
    1
    Views:
    177
    Evertjan.
    Sep 30, 2005
Loading...

Share This Page