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 2 of 2
  1. #1
    ubh
    ubh is offline
    Regular Coder ubh's Avatar
    Join Date
    Apr 2008
    Location
    Portland, Oregon U.S.A.
    Posts
    443
    Thanks
    108
    Thanked 15 Times in 14 Posts

    Explode and replace

    Say I have a string of text:
    Code:
    Hello world, this is my string of text that I would like to use and it contains 
    copy with indicators such as {{this}}. How would I go about exploding 
    these {{indicators}} out of the string to replace the {{indicators}}
     with html tags such as <strong></strong>?
    So again is there a way to explode that string and replace all {{}} indicators with html elements and then rebuild the string back together?

  • #2
    Regular Coder Amphiluke's Avatar
    Join Date
    Jul 2009
    Posts
    312
    Thanks
    3
    Thanked 89 Times in 89 Posts
    The best plan to be followed is to use regular expressions.
    Code:
    <script type="text/javascript">
       var initStr = "Hello world, this is my string of text that I would like to use and it contains copy with indicators such as {{this}}. How would I go about exploding these {{indicators}} out of the string to replace the {{indicators}} with html tags such as <strong></strong>?";
       var re = new RegExp("\{\{([^\{\}]*)\}\}", "g");
       var resultStr = initStr.replace(re, "<strong>$1</strong>");
       document.write(resultStr);
    </script>
    I am still learning English


  •  

    Posting Permissions

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