Remove all BUT the characters of a given string from a string - Node.js String

Node.js examples for String:Char

Description

Remove all BUT the characters of a given string from a string

Demo Code

/* Remove all BUT the characters of a given string from a string
 *
 * "Abracadabra".remove('ar') => "raaara"
 *///w  w w  .ja  va 2 s .c om
String.prototype.filterchars = function ( chars ) {
  if ( arguments.length > 1 ) {
    chars = Array.prototype.join.call( arguments, '' );
  }
  var rx = RegExp.compile('[^'+RegExp.escape(chars)+']', 'g');
  return this.replace( rx, '' );
};

Related Tutorials