Just some JavaScript code to strip whitespace - with thanks to Josh Twist


//strips whitespace from the ends of a string.
function trim(string)
{
	var re= /^\s\s*(.*)\s*\s$/;
	return string.replace(re,"$1");
}

		

That's useful    !!

BUT....

That only works when you have leading and trailing spaces... so change the expression to

/^\s*(.*)\s*$/;
		

.....which strips the leading spaces regardless of wether or not there are trailing spaces. Still doesn't trim the trailing spaces though...

THEN....

This does...

function trim(string)
{
var re= /^\s|\s$/g;
return string.replace(re,"");
}

		

BUT....

That only works for one space at each end...

OK the final expression, which works properly is:

/^\s*|\s*$/g;

		

Thanks to Alex Shiell, ITS, EB, SE and Chris Scott for the last three !!

This should have given you some ideas about the wonders of JaveScript    !!