Return a new string with the value stripped from the left and right of the string - Node.js String

Node.js examples for String:Strip

Description

Return a new string with the value stripped from the left and right of the string

Demo Code


/**/*w w  w .  j  a v  a  2s  . c o  m*/
 * Return a new string with the value stripped from the left and right of the string
 * @version 1.1.1
 * @date July 22, 2010
 * @since 1.0.0, June 30, 2010
 * @package jquery-sparkle {@link http://www.balupton/projects/jquery-sparkle}
 * @author Benjamin "balupton" Lupton {@link http://www.balupton.com}
 * @copyright (c) 2009-2010 Benjamin Arthur Lupton {@link http://www.balupton.com}
 * @license MIT License {@link http://creativecommons.org/licenses/MIT/}
 */
String.prototype.strip = String.prototype.strip || function(value,regex){
  // Strip a value from left and right, with optional regex support (defaults to false)
  value = String(value);
  var str = this;
  if ( value.length ) {
    if ( !(regex||false) ) {
      // We must escape value as we do not want regex support
      value = value.replace(/([\[\]\(\)\^\$\.\?\|\/\\])/g, '\\$1');
    }
    str = str.replace(eval('/^'+value+'+|'+value+'+$/g'), '');
  }
  return String(str);
}

Related Tutorials