...

View Full Version : Explode and replace



ubh
07-17-2009, 06:08 AM
Say I have a string of text:


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?

Amphiluke
07-17-2009, 06:56 AM
The best plan to be followed is to use regular expressions.

<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>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum