We know that the string variable in C # has a very useful method trim (). We can use it to remove spaces at the beginning and end of the string. There is no native trim () function in JS, but we can use a regular expression + prototype to implement this function.
String. prototype. trim = function () {var Reg =/(^ \ s +) | (\ s + $)/g; return this. replace (Reg ,"");}
Note:
Add the trim method to the prototype of the string constructor so that all strings have this method.
Regular Expressions match the blank characters at both ends of a string (including tabs)
Use the string replace method to replace the blank characters matching the regular expression with the null character "".
InCodeWe can use
VaR strtest = 'I am go'; strtest = strtest. Trim (); alert ('prompt information' + strtest + ". ");
If you do not use the trim () function, the following message is displayed: I am a god.
Print when using: the prompt message is "I am a God.
A simple trick, have you learned?