Trim function in javascript to replaces leading and trailing spaces

You can define a trim function which trims leading and trailing space in javascript by using any one of the following way:

way 1:
String.prototype.trim = function() {  return this.replace(/^\s+|\s+$/g, '');  }

way 2:
function trim()
{
return this.replace(/^\s+|\s+$/g, '');
}


Following function shows how to use the trim function
function temp()
{

var str=' maninder ';
alert(str.trim());
}

you will see 'maninder' in the alert box. All the leading and trailing space are trimmed 
Ebook Download
View all
Learn
View all