---
title: "javascript 去除空格"
author: "hewj"
date: "2007-10-11T11:27:25Z"
tags: []
slug: "javascript"
draft: false
lastmod: "2007-10-11T11:27:25Z"
id: 130998
url: "https://hewj.iteye.com/blog/130998"
---

String.prototype.Trim  =  function()    
{    
return  this.replace(/(^\\s\*)  ¦(\\s\*$)/g,  "");    
}    
   
String.prototype.LTrim  =  function()    
{    
return  this.replace(/(^\\s\*)/g,  "");    
}    
   
String.prototype.RTrim  =  function()    
{    
return  this.replace(/(\\s\*$)/g,  "");    
}  

以后调用：  
var str=document.form1.UserName.value.trim();  
if(str.length==0)  
{  
   ...处理代码  
}