1. Remove all spaces before and after the string:
code show as below:
illustrate:
If you use jQuery to use the $.trim(str) method directly, str represents a string with all spaces before and after it removed.
2. Remove all spaces in the string (including intermediate spaces, you need to set the second parameter to: g)
code show as below:
3. Most browsers now basically support the trim function of strings, but in order to be compatible with unsupported browsers, we'd better add the following code to the Js file (if you don't need to clear line breaks, please delete the \n system Tab delete \t):
If you only need the trim function, you can just write one:
Use code: