show me how to replace all instances of word in textbox

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

  1. alxasa

    alxasa 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.
     
    alxasa, Dec 1, 2006
    #1
    1. Advertisements

  2. alxasa

    Jeff North Guest

    <SCRIPT>
    re = /apples/gi;
    str = "Apples are round, and apples are juicy.";
    newstr=str.replace(re, "oranges");
    document.write(newstr)
    </SCRIPT>
     
    Jeff North, Dec 1, 2006
    #2
    1. Advertisements

  3. alxasa

    alxasa Guest

    :) 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>
     
    alxasa, Dec 2, 2006
    #3
  4. alxasa

    Jeff North Guest

    <!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>
     
    Jeff North, Dec 2, 2006
    #4
  5. alxasa

    alxasa Guest

    Very very nice and elegant, Jeff. :) Could you show me how to tweak
    your code to account for case-sensitivity for changes?
     
    alxasa, Dec 2, 2006
    #5
  6. alxasa

    alxasa 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 ?
     
    alxasa, Dec 2, 2006
    #6
  7. alxasa

    Evertjan. Guest

    wrote on 02 dec 2006 in comp.lang.javascript:
    [please always quote on usenet]
     
    Evertjan., Dec 2, 2006
    #7
  8. alxasa

    alxasa Guest

    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>
     
    alxasa, Dec 2, 2006
    #8
  9. alxasa

    Evertjan. Guest

    wrote on 03 dec 2006 in comp.lang.javascript:

    var re = new Regex(top.value,'gi')
     
    Evertjan., Dec 2, 2006
    #9
  10. alxasa

    Jeff North Guest

    <!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>
     
    Jeff North, Dec 3, 2006
    #10
  11. alxasa

    alxasa Guest

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

    mick white Guest

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

    or

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

    Mick
     
    mick white, Dec 3, 2006
    #12
  13. Nonsense.


    PointedEars
     
    Thomas 'PointedEars' Lahn, Dec 8, 2006
    #13
  14. alxasa

    mick white Guest

    Not the best, perhaps, but "Nonsense"? No.
    Mick
     
    mick white, Dec 9, 2006
    #14
  15. alxasa

    Evertjan. Guest

    mick white wrote on 09 dec 2006 in comp.lang.javascript:
    Good of you to wash some ears, Mick, but eval() is evil.
     
    Evertjan., Dec 9, 2006
    #15
  16. That was premature. The constructor is RegExp().
    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
     
    Thomas 'PointedEars' Lahn, Dec 9, 2006
    #16
  17. alxasa

    mick white Guest

    Ahh, the missing "p", good catch.
    It handles control characters, "\n", "\t" etc..., a little better.
    Mick
     
    mick white, Dec 9, 2006
    #17
  18. In what way, please?


    PointedEars
     
    Thomas 'PointedEars' Lahn, Dec 9, 2006
    #18
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.