Js remove space instance Trim (), LTrim (), RTrim () need friends can come to refer to, hope to help everyone
1. Remove string spaces in js
// Remove the left space;
Function ltrim (s)
...{
Return s. replace (/(^ s *)/g ,"");
}
// Remove the right space;
Function rtrim (s)
...{
Return s. replace (/(s * $)/g ,"");
}
// Remove left and right spaces;
Function trim (s )...{
// S. replace (/(^ s *) | (s * $)/g ,"");
Return rtrim (ltrim (s ));
}
2. Remove spaces on both sides of the string.
// Parameter: string passed in by mystr
// Return: String mystr
Function trim (mystr ){
While (mystr. indexOf ("") = 0) & (mystr. length> 1 )){
Mystr = mystr. substring (1, mystr. length );
} // Remove the leading space
While (mystr. lastIndexOf ("") = mystr. length-1) & (mystr. length> 1 )){
Mystr = mystr. substring (0, mystr. length-1 );
} // Remove trailing Spaces
If (mystr = ""){
Mystr = "";
}
Return mystr;
}