JS中 String.prototype.Trim = function()
2014-09-12 10:54
204 查看
利用Javascript中每个对象(Object)的prototype属性我们可以为Javascript中的内置对象添加我们自己的方法和属性。
以下我们就用这个属性来为String对象添加三个方法:Trim,LTrim,RTrim(作用和VbScript中的同名函数一样)
CODE:
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, "");
}
怎么样,简单吧,下面看一个使用的实例:[复制此代码]
CODE:
<script language=javascript>
String.prototype.Trim = function()
{
return this.replace(/(^\s*)|(\s*$)/g, "");
}
var s = " leading and trailing spaces ";
window.alert(s + " (" + s.length + ")");
s = s.Trim();
window.alert(s + " (" + s.length + ")");
</script>
以下我们就用这个属性来为String对象添加三个方法:Trim,LTrim,RTrim(作用和VbScript中的同名函数一样)
CODE:
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, "");
}
怎么样,简单吧,下面看一个使用的实例:[复制此代码]
CODE:
<script language=javascript>
String.prototype.Trim = function()
{
return this.replace(/(^\s*)|(\s*$)/g, "");
}
var s = " leading and trailing spaces ";
window.alert(s + " (" + s.length + ")");
s = s.Trim();
window.alert(s + " (" + s.length + ")");
</script>
相关文章推荐
- String.prototype.trim=function是什么意思?
- String.prototype.trim= function(){ return this.replace(/(^\s*)|(\s*$)/g, ""); };
- String.prototype.trim=function是做什么用的?
- c++ trim function string
- Add a function called trim as a method of the prototype (similar to VBScript's Trim function)
- js 检测数据类型instanceof/constructor和 Object.prototype.toString.call()
- js的深入学习课程Object.prototype.toString.call()
- JS核心概念:Context, Scope, Function, Prototype,Object
- Prototype使用指南之string.js
- Js String原型扩展trim()
- js去掉左右两边空格 重写String的trim方法
- JS:Trim() in javascript, how to define a function of checkinput for a WebControl(ascx)
- JS常用方法--String.prototype使用!
- Js string 添加trim()方法
- JS里的prototype对JS类(方法,函数,function)进行一个扩展吧!
- Js中Prototype、__proto__、Constructor、Object、Function关系介绍
- String.prototype.trim是做什么用的?
- 四個基本的js function[$(),trim(),ltrim(),rtrim()]
- js在IE8+兼容String没有trim方法,写一个兼容ie8一下的浏览器的trim()方法
- Js中Prototype、__proto__、Constructor、Object、Function关系介绍