JavaScript Trim function with defined characters


A complete functional JavaScript function to trim string with specified character while default character is white space. You can also use underscore.string Js.

Trim Function

<script type="text/javascript">
function makeString(object) {
          if (object == null) return \'\';
          return String(object);
        };

function escapeRegExp(str) {
          return makeString(str).replace(/([.*+?^=!:${}()|[]/\\])/g, \'\\$1\');
        };
        
function defaultToWhiteSpace(characters) {
          if (characters == null)
            return \'\\s\';
          else if (characters.source)
            return characters.source;
          else
            return \'[\' + escapeRegExp(characters) + \']\';
        };      
        
function ltrim(str, characters) {
        var nativeTrimLeft = String.prototype.trimLeft;
          str = makeString(str);
          if (!characters && nativeTrimLeft) return nativeTrimLeft.call(str);
          characters = defaultToWhiteSpace(characters);
          return str.replace(new RegExp(\'^\' + characters + \'+\'), \'\');
        };

function trim(str, characters) {
        var nativeTrim = String.prototype.trim;
          str = makeString(str);
          if (!characters && nativeTrim) return nativeTrim.call(str);
          characters = defaultToWhiteSpace(characters);
          return str.replace(new RegExp(\'^\' + characters + \'+|\' + characters + \'+$\', \'g\'), \'\');
        };
        
function rtrim(str, characters) {
        var nativeTrimRight = String.prototype.trimRight;
          str = makeString(str);
          if (!characters && nativeTrimRight) return nativeTrimRight.call(str);
          characters = defaultToWhiteSpace(characters);
          return str.replace(new RegExp(characters + \'+$\'), \'\');
        };

//test function
alert(rtrim(\'   
hi   how   are       you   \')+\' \'+ rtrim("_-foobar-_", "_-"));
    
        
</script>

Here is ouput

js example

RTrim Function