User login

A JavaScript Trim function

The following JavaScript function removes both leading and trailing spaces.

function trim(strText) {
while (strText.substring(0,1) == ' ')
strText = strText.substring(1, strText.length);
while (strText.substring(strText.length-1,strText.length) == ' ')
strText = strText.substring(0, strText.length-1);
return strText;
}

Here is a version that uses a regular expression:

str = " test ";
str = str.replace(/^\s*|\s*$/g,"");

The regular expression version works for any whitespace characters like tabs, newlines, etc.
Another example of a regular expression trim function is at Breaking Par site.