Last active
August 29, 2015 14:15
-
-
Save geedew/58d6b79a4525d27a774b to your computer and use it in GitHub Desktop.
JavaScript String.Strip
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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