Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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