davesmiths
2/19/2014 - 12:54 PM

Trim white space or a given character from the beginning and end of a string, JavaScript

Trim white space or a given character from the beginning and end of a string, JavaScript

trim = function(str, char) {
	char = (char || ' ').replace(/([\[\]\(\)])/g, '\\$1');
	return str.replace(new RegExp("^"+char+"+"), '').replace(new RegExp(char+"+$"), '');
};