Tuesday, October 6, 2015

Remove Trailing Characters with Javascript

This is not CF but it sure comes in handy.

If for any reason you need to get rid pesky trailing characters, here's the script:

<script>
function trimTrailingChars(s, charToTrim)
   {
     var regExp = new RegExp(charToTrim + "+$");
     var result = s.replace(regExp, "");

     return result;
   }
</script>




And here is how you use it in your code:

var sText = trimTrailingChars(sText, ',');


Note the red text above. Change the comma to whatever you want to get rid of.