js空格处理函数
来源:互联网 发布:淘宝网怎么买二手商品 编辑:程序博客网 时间:2024/05/16 04:35
來源:http://www.happyshow.org/view.php?id=114
去除字符串左右两端的空格,在vbscript里面可以轻松地使用 trim、ltrim 或 rtrim,但在js中却没有这3个内置方法,需要手工编写。下面的实现方法是用到了正则表达式,效率不错,并把这三个方法加入String对象的内置方法中去。
<input type="text" name="mytxt" value=" 12345678 " /><br>
<input type="button" name="cmd1" onclick="mytxt2.value=mytxt.value.trim()" value="去两边的空格"/>
<input type="text" name="mytxt2"/><br>
<input type="button" name="cmd1" onclick="mytxt3.value=mytxt.value.ltrim()" value="去左边的空格"/>
<input type="text" name="mytxt3"/><br>
<input type="button" name="cmd1" onclick="mytxt4.value=mytxt.value.rtrim()" value="去右边的空格"/>
<input type="text" name="mytxt4"/><br>
<input type="button" name="cmd1" onclick="mytxt2.value=mytxt.value.trim()" value="去两边的空格"/>
<input type="text" name="mytxt2"/><br>
<input type="button" name="cmd1" onclick="mytxt3.value=mytxt.value.ltrim()" value="去左边的空格"/>
<input type="text" name="mytxt3"/><br>
<input type="button" name="cmd1" onclick="mytxt4.value=mytxt.value.rtrim()" value="去右边的空格"/>
<input type="text" name="mytxt4"/><br>
<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>
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>
写成函数可以这样:
<script type="text/javascript">
function trim(str){ //删除左右两端的空格
return str.replace(/(^s*)|(s*$)/g, "");
}
function ltrim(str){ //删除左边的空格
return str.replace(/(^s*)/g,"");
}
function rtrim(str){ //删除右边的空格
return str.replace(/(s*$)/g,"");
}
</script>
function trim(str){ //删除左右两端的空格
return str.replace(/(^s*)|(s*$)/g, "");
}
function ltrim(str){ //删除左边的空格
return str.replace(/(^s*)/g,"");
}
function rtrim(str){ //删除右边的空格
return str.replace(/(s*$)/g,"");
}
</script>
- js空格处理函数
- js空格处理函数
- js处理空格的函数
- js空格处理
- JS 去空格函数
- js去空格函数
- js去除空格函数
- bat函数处理变量空格
- 精通js之 处理DOM中的空格
- js关于字符串空格的处理
- 字符串处理函数 TRIM - 去掉首尾空格
- c语言处理字符中的空格 函数
- js去空格函数(方法)
- js去空格、去重函数
- js字符串处理函数
- JS字符串处理函数
- JS Date函数处理
- JS事件处理函数
- SQL Server 安装程序无法获取 ASPNET 帐户的系统帐户信息。若要继续,请重新安装 .NET Framework,然后再次运行 SQL 。
- 实现中文字串截取无乱码的方法php
- 想像五年后的你【转帖】
- ADO.NET Entity Framework step by step
- 外行眼中的语言
- js空格处理函数
- 格式化数值 C# int.ToString()
- 离散数学中等价关系的认识
- Oracle 10g for linux X86的SILENT安装
- (编程题目)约瑟夫问题(实用C++编出一个程序解决约瑟夫问题)--凌风
- 用jquery + iframe實現iframe子頁面加載完前的緩沖效果
- 基于文档的新一代数据库系统——CouchDB
- 大家来找茬辅助工具实现
- 把所有的字体名称放到 ComboBox自动查询