Skip to content

Instantly share code, notes, and snippets.

@geedew
Last active August 29, 2015 14:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save geedew/58d6b79a4525d27a774b to your computer and use it in GitHub Desktop.
Save geedew/58d6b79a4525d27a774b to your computer and use it in GitHub Desktop.
JavaScript String.Strip
var strip = function(string, characters) {
if(!characters) {
if(typeof String.prototype.trim !== undefined) {
// Simply use the String.trim as a default
return String.prototype.trim.call(string);
} else {
// set characters to whitespaces
characters = "\s\uFEFF\xA0";
}
}
// Characters is set at this point forward
// Validate characters just in case there are invalid usages
var escaped = characters.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, '\\$1');
var target = new RegExp('^[' + escaped + ']+|[' + escaped + ']+$');
// Remove the characters from the string
return string.replace(target, '');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment