Javascript Equivalent of PHP’s trim function

Trim function in PHP can remove whitespace from the beginning and end of a string.

Javascript do not have this function, but is easy to made one.

// remove space in a string 
function trim(str)
{
	if(!str || typeof str != 'string')
		return null;
		
	return str.replace('/^\s+|\s+$/g', '');
}
affiliate_link
Share this Post:
Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

No Responses to “Javascript Equivalent of PHP’s trim function”

Leave a Reply:

Name (required):
Mail (will not be published) (required):
Website:
Comment (required):
XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>