Thursday, September 25, 2008

Replace tokens in a string

Here is a usefull litle function which replaces values in a string:



 function replaceTokens(sString, aTokens) {
   var oRe;
   var sResult = sString;
   for(var iToken=0; iToken<aTokens.length; iToken++) {
      oRe = new RegExp('\\u007b'+iToken+'\\u007d', 'ig');
      try {
         sResult = sResult.replace(oRe, aTokens[iToken]);
      } catch(e) {
      }
   }
   return sResult;
 }

No comments: