在Javascript中为String对象添加trim,ltrim,rtrim方法_Javascript教程-查字典教程网
在Javascript中为String对象添加trim,ltrim,rtrim方法
在Javascript中为String对象添加trim,ltrim,rtrim方法
发布时间:2016-12-30 来源:查字典编辑
摘要:以下我们就用这个属性来为String对象添加三个方法:Trim,LTrim,RTrim(作用和VbScript中的同名函数一样)复制代码代码...

以下我们就用这个属性来为String对象添加三个方法:Trim,LTrim,RTrim(作用和VbScript中的同名函数一样)

复制代码 代码如下:String.prototype.Trim=function()

{

returnthis.replace(/(^s*)|(s*$)/g,"");

}

String.prototype.LTrim=function()

{

returnthis.replace(/(^s*)/g,"");

}

String.prototype.Rtrim=function()

{

returnthis.replace(/(s*$)/g,"");

}

怎么样,简单吧,下面看一个使用的实例:复制代码 代码如下:

<scriptlanguage=javascript>

String.prototype.Trim=function()

{

returnthis.replace(/(^s*)|(s*$)/g,"");

}

vars="leadingandtrailingspaces";

window.alert(s+"("+s.length+")");

s=s.Trim();

window.alert(s+"("+s.length+")");

</script>

相关阅读
推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
  • 大家都在看
  • 小编推荐
  • 猜你喜欢
  • 最新Javascript教程学习
    热门Javascript教程学习
    编程开发子分类