1. remove all spaces before and after the string :
--JS to achieve the trim function
Remove all empty strings before and after the string
function Trim (str) { return str.replace (/(^\s*) | ( \s*$)/g, ""); }
Adding a method to a string prototype can also
String.prototype.Trim = function ()
{
Return This.replace (/(^\s*) | ( \s*$)/g, "");
}
Remove Space before string
function () { returnthis. Replace (/(^\s*)/g, "");}
Remove space after string
function () { returnthis. Replace (/(\s*$)/g, "");}
Description:
If you use jquery to directly use the $. Trim(str) method, str indicates that you want to remove all the spaces before and after the string.
--function to remove whitespace on both sides of a string
Parameter: MyStr passed in string
Return: String mystr
// Remove Front spaces function Trim (mystr) { while ((Mystr.indexof (") ==0) && (mystr.length>1 )) { mystr =mystr.substring (1, mystr.length); }
Remove trailing spaces
while ((Mystr.lastindexof (") ==mystr.length-1) && (mystr.length>1)) {
Mystr=mystr.substring (0,mystr.length-1);
}
if (mystr== "") {
Mystr= "";
}
return mystr;
}
str = str.replace (/[]/g, "");//Replace all blanks//Erase All BlanksfunctionAllTrim (s) {varS1 =LTrim (s); return(RTrim (S1));}//Delete left blank (with Fullwidth blank)functionLTrim (s) {s= "" +s; varLen =s.length; varpos = 0; varS1 = ""; while(Pos <Len) { varCHR = s.substring (pos, pos + 1); if(Chr = = "" | | CHR = = "" | | CHR = = "") Pos= pos + 1; Else return(s.substring (Pos,len)); } returnS1;}//Remove right margin (with Fullwidth Blanks)functionRTrim (s) {s= "" +s; varLen =s.length; varpos = len-1; varS1 = ""; vari = 0; while(POS >= 0) { varCHR = s.substring (pos, s.length-i); if(Chr = = "" | | CHR = = "" | | CHR = = "") Pos= Pos-1; Else return(s.substring (0, POS +1)); I++; } returnS1;}//Remove extra space in the string (keep a blank)functionMiddletrim (str) {varRepstr= ""; for(vari=0;i<str.length;i++) { if(Str.substring (i,i+1)! = "") Repstr=repstr+str.substring (i,i+1); Else { if(Repstr.substring (repstr.length-1,repstr.length)! = "") {Repstr=repstr+ ""; } } } returnrepstr;}
2, Remove all the spaces in the string (including the middle space, you need to set the 2nd parameter is: g)
function Trim (Str,is_global) { var result; = Str.replace (/(^\s+) | ( \s+$)/g, ""); if (is_global.tolowercase () = = "G") { = Result.replace (/\s/g, ""); } return result;}
Use JS to remove the front and back space or the middle space Daquan