Today, I helped my colleagues solve a page problem and encountered a common public custom function trim (remove the leading and trailing spaces)
Function trim (STR) {<br/> var Re = // s * (/s [^/0] */s)/S */; <br/> re.exe C (STR); <br/> return Regexp. $1; <br/>}< br/> alert (TRIM ("1234"); <br/> alert (TRIM ("")); <br/>/* <br/> recommended method <br/> function trim (STR) {<br/> return Str. replace (/^/S + |/S + $/, ""); <br/>}< br/> */
It is found that if the parameter is a null string, the output result is the string O (character □character) O of the previous test.
Simplify some test code:
/(/D + )/. exec ("12345"); <br/> alert (Regexp. $1); <br/>/(/d + )/. exec ("ABCDE"); <br/> alert (Regexp. $1 );
The result is clear. If the regular expression does not match the string, the group attribute of the Regexp object is not changed.
Function trim (STR) {<br/> var Re = // s * (/s [^/0] */s)/S */; <br/> If (! Re.exe C (STR) return STR; <br/> return Regexp. $1; <br/>}
The solution is to add a judgment.