JS 去空格

来源:互联网 发布:手机淘宝开店加载不了 编辑:程序博客网 时间:2024/05/16 09:49
  1. // JS去掉左边空格  
  2. function lTrim(str)  
  3. {  
  4.     if ((typeof(str) != "string") || !str)  
  5.     {  
  6.         return "";   
  7.     }  
  8.     return str.replace(/(^\s*)/g, "");   
  9. }  
  10.   
  11. // JS去掉右边空格  
  12. function rTrim(str)  
  13. {  
  14.     if ((typeof(str) != "string") || !str)  
  15.     {  
  16.         return "";  
  17.     }  
  18.     return str.replace(/(\s*$)/g, "");   
  19. }  
  20.   
  21. // JS去掉两边空格  
  22. function trimStr(str)  
  23. {   
  24.     if ((typeof(str) != "string") || !str)  
  25.     {  
  26.         return "";   
  27.     }  
  28.     return str.replace(/(^\s*)|(\s*$)/g, "");   
  29. }