Monday, February 21, 2011

JavaScript - Trim a string

I need to trim strings in JavaScript every day and everytime I forgot the code snippet that do that so here is

/**Trims a string
*@param {String} str - the string to trim
*@return {String} the trimmed string
function trim(str){
var res = str.replace(/^\s+/, '');
res = res.replace(/\s+$/, '');

return res;

Pfui, now it's here forever.

No comments:

Post a Comment