Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  javascript  >  正文 js去除空格的代码(前后空格、前空格、后空格)

js去除空格的代码(前后空格、前空格、后空格)

发布时间:2014-09-03   编辑:www.jquerycn.cn
js去除空格的代码,包括前后都是空格,前空格,后空格等的去除方法,有需要的朋友,可以参考下。

js去除空格方法示例。
 

复制代码 代码示例:
<script>
<!--
//出处:网上搜集
// Trim() , Ltrim() , RTrim()
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, "");
} //by http://www.jquerycn.cn
//-->
</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的正则表达式去除空格,供大家参考。

您可能感兴趣的文章:
js 禁止文本框输入空格的代码
js去掉空格的代码
js去除空格的代码(前后空格、前空格、后空格)
js去掉字符串的空格或换行符(附相关正则介绍)
JS去除字符串的中间空格的代码
js去空格技巧 js去除字符串前后、左右空格
js trim函数 js去空格函数与正则
js实现trim函数实例代码
jquery 去空格的方法大全
js/jquery去掉空格,回车,换行示例代码

关键词: js去空格   
[关闭]