1、直接上代码
function trim(pValue){
return pValue.replace(/^[\s]*/gi,"").replace(/[\s]*$/gi,"");
}
2、用法

3、效果

4、成功去掉两端空格
5、<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<button onclick="fun()">测试</button>
<script type="text/javascript">
function fun(){
alert(trim(' 你好我前面和后面都有空格 '))//id)
}
function trim(pValue){
return pValue.replace(/^[\s]*/gi,"").replace(/[\s]*$/gi,"");
}
</script>
</body>
</html>