Example: Enter a space to give hints
The code is as follows |
Copy Code |
Verify that the content contains spaces function Checktextspace (obj,temp) { var reg=/(^s+) | (s+$)/g; var alertvalue= "Input content contains spaces, please enter new input!"; Temp is used to identify whether the content allows space 1 to exist for 0 to be non-existent if (temp==1) { reg=/(^s{5,}) | (s{5,}$) | (S{5,})/g; Alertvalue= "The number of consecutive spaces in the content of more than 5, please re-enter!" "; } if (Reg.test (Obj.value)) { alert (Alertvalue); Obj.focus (); return false; } } |
other ways to remove spaces from different locations
1. Eliminate the space on the left side of the string
The code is as follows |
Copy Code |
function Lefttrim (str) { Return Str.replace (/^s*/, "");//^ symbol indicates matching from the beginning to the left } Alert ("A" +lefttrim ("AAA") + "111"), or 111 on both sides as a reference to determine whether the space is deleted |
2. Eliminate the space on the right side of the string
The code is as follows |
Copy Code |
function Righttrim (str) { Return Str.replace (/s*$/, ""); } Alert ("A" +righttrim ("AAA") + "111"), or 111 on both sides as a reference to determine whether the space is deleted |
3. Eliminate space on both sides of the string
The code is as follows |
Copy Code |
function Trim (str) { Return Str.replace (/(^s*) | ( s*$)/g, ""); } Alert ("A" +trim ("AAA") + "111"), or 111 on both sides as a reference to determine whether the space is deleted |
Of course, in order to facilitate, we can also expand the function in the string
The code is as follows |
Copy Code |
String.prototype.trim = function () { Return trim (this); } var str = "AAA"; Alert ("+str.trim" () + "111"), or 111 on both sides as a reference to determine whether the space is deleted |
Full instance
The code is as follows |
Copy Code |
<script type= "Text/javascript" language= "JavaScript" > var s = "FOo bar"; for (var index = 0; index < s.length; index++) { Alert (s.charcodeat (index)); Alert (S.charat (index)); } Remove the left space function LTrim (s) { var flg = 0; var lstr = '; while (s.charcodeat (FLG) = =) flg++; alert (FLG); for (var index = 0; index < S.LENGTH-FLG; index++) lstr+= S.charat (Flg+index); return lstr; } Remove the space to the right function RTrim (s) { var flg = 0; var rstr = '; var strlength = s.length; while (S.charat (STRLENGTH-1-FLG) = = ") flg++; for (var index = 0; index < S.LENGTH-FLG; index++) rstr+= S.charat (index); return RSTR; } Remove a space with a regular expression function Replacespace (str) { Return Str.replace (/s/g, ""); } Remove all spaces function Removeallspace (str) { var localstring = '; for (var index = 0; index<str.length; index++) if (str.charcodeat (index)!= 32) { Localstring + + str.charat (index); }; return localstring; } </script> |
^ Start of Match string
$ end of Match string
/s matches any white space character
/(^s+) | (s+$)/g This is the matching content contains spaces, whether before or after the middle, can match to
/(^s{5,}) | (s{5,}$) | (S{5,})/g This is what I do another validation change, mainly to match the number of consecutive input spaces
S{5,} This representation matches 5 or more times
S* the representative repeats 0 or more times.
S+ the representative repeats 1 or more times.
S. This representative repeats 0 or 1 times.