String
Adds a trim method to the String object prototype, removing the space before and after the string
String.prototype.trim = function ()
{
Use a regular expression to replace the front and back spaces with an empty string.
Return This.replace (/(^\s*) | ( \s*$)/g, "");
}
//------------------------------------------------------------------------------------------------------
Example:
<script>
String.prototype.trim = function ()
{
Use a regular expression to replace the front and back spaces with an empty string.
Return This.replace (/(^\s*) | ( \s*$)/g, "");
}
A string with a space
var s = "Leading and trailing spaces";
Show "leading and trailing spaces (35)"
Window.alert (S + "(" + s.length +) ");
Remove before and after spaces
s = S.trim ();
Show "leading and trailing spaces (27)"
Window.alert (S + "(" + s.length +) ");
</script>