JavaScript does not have a native trim function. Creating a trim function for use in JavaScript is not difficult, however.
Example:
function trim(value)
{
//Takes any spaces at the front of the string (^)
//or (’|') at the end of the string ($)
//and replaces them with an empty string
var strValue = value.replace(/^\s+|\s+$/,'');
return strValue;
}
var TrimThis = ' JavaScript trim ';
//Before trim is applied on the string,
//the character at position 0 is a space
document.write(TrimThis.charAt(0));
var TrimmedString = trim(TrimThis);
//After the trim is applied,
//the character at position 0 is J
document.write(TrimmedString.charAt(0));
Filed under: JavaScript, Programming, Software, Web