手机
当前位置:查字典教程网 >编程开发 >Javascript教程 >javascript String 的扩展方法集合
javascript String 的扩展方法集合
摘要://获取字符数组String.prototype.ToCharArray=function(){returnthis.split("");}...

//获取字符数组

String.prototype.ToCharArray=function()

{

returnthis.split("");

}

//获取N个相同的字符串

String.prototype.Repeat=function(num)

{

vartmpArr=[];

for(vari=0;i<num;i++)tmpArr.push(this);

returntmpArr.join("");

}

//逆序

String.prototype.Reverse=function()

{

returnthis.split("").reverse().join("");

}

//测试是否是数字

String.prototype.IsNumeric=function()

{

vartmpFloat=parseFloat(this);

if(isNaN(tmpFloat))returnfalse;

vartmpLen=this.length-tmpFloat.toString().length;

returntmpFloat+"0".Repeat(tmpLen)==this;

}

//测试是否是整数

String.prototype.IsInt=function()

{

if(this=="NaN")returnfalse;

returnthis==parseInt(this).toString();

}

//合并多个空白为一个空白

String.prototype.resetBlank=function()

{

returnthis.replace(/s+/g,"");

}

//除去左边空白

String.prototype.LTrim=function()

{

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

}

//除去右边空白

String.prototype.RTrim=function()

{

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

}

//除去两边空白

String.prototype.trim=function()

{

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

}

//保留数字

String.prototype.getNum=function()

{

returnthis.replace(/[^d]/g,"");

}

//保留字母

String.prototype.getEn=function()

{

returnthis.replace(/[^A-Za-z]/g,"");

}

//保留中文

String.prototype.getCn=function()

{

returnthis.replace(/[^u4e00-u9fa5uf900-ufa2d]/g,"");

}

//得到字节长度

String.prototype.getRealLength=function()

{

returnthis.replace(/[^x00-xff]/g,"--").length;

}

//从左截取指定长度的字串

String.prototype.left=function(n)

{

returnthis.slice(0,n);

}

//从右截取指定长度的字串

String.prototype.right=function(n)

{

returnthis.slice(this.length-n);

}

//HTML编码

String.prototype.HTMLEncode=function()

{

varre=this;

varq1=[/x26/g,/x3C/g,/x3E/g,/x20/g];

varq2=["&","<",">",""];

for(vari=0;i<q1.length;i++)

re=re.replace(q1[i],q2[i]);

returnre;

}

//Unicode转化

String.prototype.ascW=function()

{

varstrText="";

for(vari=0;i<this.length;i++)strText+=""+this.charCodeAt(i)+";";

returnstrText;

}

【javascript String 的扩展方法集合】相关文章:

删除javascript所创建子节点的方法

javascript先序遍历DOM树的方法

JavaScript实现鼠标拖动效果的方法

javascript获取select值的方法分析

JavaScript中的Math.LN2属性用法详解

javascript中一些util方法汇总

JavaScript数据结构与算法之集合(Set)

JavaScript操作Cookie方法实例分析

Node.js中JavaScript操作MySQL的常用方法整理

javascript元素动态创建实现方法

精品推荐
分类导航