Online JavaScript Tutorials

JavaScript tutorials which will help you use javaScripts on your web pages, even beginners. This javascript tutorial also includes links to other programming sites.

JavaScript trim function Equivalent of PHP trim Function

This function returns a string with whitespace stripped/removed from the beginning and end of string . Without the second parameter, trim() will strip these characters:

* " " (ASCII 32 (0x20)), an ordinary space.
* "\t" (ASCII 9 (0x09)), a tab.
* "\n" (ASCII 10 (0x0A)), a new line (line feed).
* "\r" (ASCII 13 (0x0D)), a carriage return.
* "\0" (ASCII 0 (0x00)), the NUL-byte.
* "\x0B" (ASCII 11 (0x0B)), a vertical tab.

In PHP we can easily use trim($string) and all whitespace are stripped And in JavaScript we use this function to stripped the White spaces.

function trim(sValue)
{
return sValue.replace(/^\s+|\s+$/g, "");
}

1 comment:

Unknown said...

I truly like to reading your post. Thank you so much for taking the time to share such a nice information.
web grid