Go to the left space;
function LTrim (s)
{
Return S.replace (/(^s*)/g, "");
}
Go to the right space;
function RTrim (s)
{
Return S.replace (/(s*$)/g, "");
}
Go to the left and right space;
function Trim (s) {
Return S.replace (/(^s*) | ( s*$)/g, "");
}
Remove the spaces at both ends of the string
String.prototype.trim=function () {return this.replace (^/s*) | ( /s*$)/g, ');
Remove all spaces from the string:
String.prototype.sTrim = function () {return this.replace (//s/g, ');}
Or:
var s = "ASD ddd bbb SSS";
var reg =//s/g;
var ss = S.replace (Reg, "");
Alert (ss);
The following is an enhanced version
Function: 1 Remove all space before and after the string
2 Remove all spaces in the string (including the middle space, you need to set the 2nd parameter is: g)
function Trim (Str,is_global)
{
var result;
result = Str.replace (/(^s+) | ( s+$)/g, "");
if (is_global.tolowercase () = = "G")
result = Result.replace (/s/g, "");
return result;
}
jquery Trim function
$.trim (str)
Most efficient when working with long strings.
<script type= "Text/javascript" >
Remove Space
function Systemtrim (str) {
var regExp =/(^s*) | (s*$)/;
Return Str.replace (RegExp, "");
}
Call
function Add ()
{
var Ownername=document.form1.ownername.value;
if (Systemtrim (ownername) = "")
{
Alert ("Please enter your name!") ");
Return
}
}
<script>