Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Feb 2013
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Need help with find and replace coding

    Hi,

    So I'm kind of new to JavaScript and I need to create a script that will grab the code I paste in a HTML page and do a few find and replace

    When I use this code
    Code:
    test1 = text.replace(/test1/, "<abbr>test_new1</abbr>");
    document.getElementById('input_output').value = test1;
    it works for but as soon as I add another line like this

    test1 = text.replace(/test1/, "<abbr>test_new1</abbr>");
    document.getElementById('input_output').value = test1;

    test2 = text.replace(/test2/, "<abbr>test_new2</abbr>");
    document.getElementById('input_output').value = test2;


    it doesn't work for some reason.

    It's very unfortunate because I need to do this like 30-40 times.

    I included my file so it'll be easier for you.

    This is just a small application to help my team internally it's nothing that'll go online

    Thanks!!!!!!!
    Attached Files Attached Files

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,893
    Thanks
    202
    Thanked 2,528 Times in 2,506 Posts
    Code:
    <div id = "input_output"> Here is test1 and also test2</div>
    
    <script type = "text/javascript">
    var text = document.getElementById("input_output").innerHTML;
    var reptext =  text.replace(/\btest1\b/, "<abbr>test_new1</abbr>");
    reptext = reptext.replace(/\btest2\b/, "<abbr>test_new2</abbr>");
    alert (reptext);  
    document.getElementById('input_output').innerHTML = reptext;
    </script
    If the text is in a textarea then you want to replace .innerHTML with .value

    Note the \b tags indicating a word boundary (whole words only).

    Quizmaster: Who is married to the Duke of Edinburgh?
    Contestant: Kate Winslet.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New Coder
    Join Date
    Feb 2013
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Wow this is exactly what I needed thank you very much!!

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,893
    Thanks
    202
    Thanked 2,528 Times in 2,506 Posts
    Glad to hear it! You may wish to add g and i flags to make the replace global (not just the first instance) and case-insensitive.

    Code:
    <script type = "text/javascript">
    var text = document.getElementById("input_output").innerHTML;
    var reptext =  text.replace(/\btest1\b/gi, "<abbr>test_new1</abbr>");
    reptext = reptext.replace(/\btest2\b/gi, "<abbr>test_new2</abbr>");
    alert (reptext);  
    document.getElementById('input_output').innerHTML = reptext;
    </script>

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    New Coder
    Join Date
    Feb 2013
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts
    That's exactly what I did, thanks again, much appreciated


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •