There is no ready-made trim () function in Javascript that can be used. You can use the following custom function to implement it. You can implement more functions by changing the regular expression.
String. Prototype. Trim = function ()
{
// Use a regular expression to separate spaces
// Replace it with a null string.
Return this. Replace (/(^ \ s *) | (\ s * $)/g ,"");
}
ProgramThe analysis is as follows:
First, replace/(^ \ s *) | (\ s * $)/g ""
Then, in/.../g, it indicates the place where the wildcard is placed, and G indicates the global parameter.
(^ \ S *) or (\ s * $) will be replaced ""
Regular Expression matching the first and last blank characters: ^ \ s * | \ s * $
It can be used to delete spaces (including spaces, tabs, form breaks, and so on) at the beginning and end of a line. It is a very useful expression.
Source: It Media Network (www.cniter.com)