way 1:
JavaScript
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); }way 2:
JavaScript
function trim()
{
return this.replace(/^\s+|\s+$/g, '');
}Following function shows how to use the trim function
Code
function temp()
{
var str=' maninder ';
alert(str.trim());
}you will see 'maninder' in the alert box. All the leading and trailing space are trimmed


Join the conversation! Your thoughts help the community grow.