在Javascript中为String对象添加trim,ltrim,rtrim方法
2008-04-29 23:32
881 查看
利用Javascript中每个对象(Object)的prototype属性我们可以为Javascript中的内置对象添加我们自己的方法和属性。
以下我们就用这个属性来为String对象添加三个方法:trim,Ltrim,Rtrim(作用和VbScript中的同名函数一样)
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 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中的同名函数一样)
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 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>
相关文章推荐
- 在Javascript中为String对象添加trim,ltrim,rtrim方法
- 在Javascript中为String对象添加trim,ltrim,rtrim方法
- JavaScript中为String对象添加 trim / ltrim / rtrim 方法
- 在Javascript中为String对象添加trim,ltrim,rtrim方法
- 在Javascript中为String对象添加trim,ltrim,rtrim方法
- 在Javascript中为String对象添加trim,ltrim,rtrim方法
- 在Javascript中为String对象添加trim,ltrim,rtrim方法
- 在Javascript中为String对象添加trim,ltrim,rtrim方法
- 在Javascript中为String对象添加trim,ltrim,rtrim方法
- 在Javascript中为String对象添加trim,ltrim,rtrim方法
- 在Javascript中为String对象添加trim,ltrim,rtrim方法
- 在Javascript中为String对象添加trim,ltrim,rtrim方法
- 在Javascript中为String对象添加trim,ltrim,rtrim方法
- 在Javascript中为String对象添加trim,ltrim,rtrim方法
- JavaScript--在Javascript中为String对象添加trim,ltrim,rtrim方法
- JavaScript--在Javascript中为String对象添加trim,ltrim,rtrim方法
- 在Javascript中为String对象添加trim,ltrim,rtrim
- Javascript中为String对象添加 Trim() 方法
- 在javascript中给string对象填加trim,ltrim,rtrim方向
- JavaScript自定义方法实现trim()、Ltrim()、Rtrim()的功能