徐亚东

您当前位置:YaDong网 >> 一些日志 >> Web前端 >> 浏览文章

js去除空格

作者:佚名   更新日期:2012年06月18日 点击:

str为要去除空格的字符串:

去除所有空格:
str = str.replace(/\s+/g,"");      

去除两头空格:
str = str.replace(/^\s+|\s+$/g,"");

去除左空格:
str = str.replace( /^\s*/, '');

去除右空格:
str = str.replace(/(\s*$)/g, "");

<SCRIPT LANGUAGE="JavaScript">   
<!--   
//出处:网上搜集   
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, "");   
}   
//-->   
</SCRIPT>   

<input type="text" value="    前后都是空格     " id="space">   

<input type="button" value="去前后空格" onclick="javascript:document.getElementById('space').value=document.getElementById('space').value.Trim();document.getElementById('space').select();">   

<input type="button" value="去前空格" onclick="javascript:document.getElementById('space').value=document.getElementById('space').value.LTrim();document.getElementById('space').select();">   

<input type="button" value="去后空格" onclick="javascript:document.getElementById('space').value=document.getElementById('space').value.RTrim();document.getElementById('space').select();">   

<input type="button" value="还原" onclick="javascript:document.getElementById('space').value='      前后都是空格     ';">    

 


 

js,javascript,js去除空格,javascript去除空格,YaDong网,徐亚东,亚东网
上一篇:只能输入数字, 正则限制input框只能输入数字,英文等
下一篇:有道身份证查询接口API

网友评论:


生活随笔

SqlServer相关

Web前端

ASP.NET编程

互联网杂谈

我们都是80后

网站建设相关