码迷,mamicode.com
首页 > Web开发 > 详细

原生js的String类扩展

时间:2015-07-13 20:20:45      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:

文章转自:http://www.cnblogs.com/zfc2201/archive/2012/12/16/2820335.html

JS String类拓展方法:

 1 //获取字符数组 
 2 String.prototype.toCharArray = function() {
 3     return this.split("");
 4 }
 5 //获取N个相同的字符串 
 6 String.prototype.repeat = function(num) {
 7     var tmpArr = [];
 8     for ( var i = 0; i < num; i++)
 9         tmpArr.push(this);
10     return tmpArr.join("");
11 }
12 //逆序 
13 String.prototype.reverse = function() {
14     return this.split("").reverse().join("");
15 }
16 //测试是否是数字 
17 String.prototype.isNumeric = function() {
18     var tmpFloat = parseFloat(this);
19     if (isNaN(tmpFloat))
20         return false;
21     var tmpLen = this.length - tmpFloat.toString().length;
22     return tmpFloat + "0".Repeat(tmpLen) == this;
23 }
24 //测试是否是整数 
25 String.prototype.isInt = function() {
26     if (this == "NaN")
27         return false;
28     return this == parseInt(this).toString();
29 }
30 // 合并多个空白为一个空白 
31 String.prototype.resetBlank = function() {
32     return this.replace(/s+/g, " ");
33 }
34 // 除去左边空白 
35 String.prototype.LTrim = function() {
36     return this.replace(/^s+/g, "");
37 }
38 // 除去右边空白 
39 String.prototype.RTrim = function() {
40     return this.replace(/s+$/g, "");
41 }
42 // 除去两边空白 
43 String.prototype.trim = function() {
44     return this.replace(/(^s+)|(s+$)/g, "");
45 }
46 // 保留数字 
47 String.prototype.getNum = function() {
48     return this.replace(/[^d]/g, "");
49 }
50 // 保留字母 
51 String.prototype.getEn = function() {
52     return this.replace(/[^A-Za-z]/g, "");
53 }
54 // 保留中文 
55 String.prototype.getCn = function() {
56     return this.replace(/[^u4e00-u9fa5uf900-ufa2d]/g, "");
57 }
58 // 得到字节长度 
59 String.prototype.getRealLength = function() {
60     return this.replace(/[^x00-xff]/g, "--").length;
61 }
62 // 从左截取指定长度的字串 
63 String.prototype.left = function(n) {
64     return this.slice(0, n);
65 }
66 // 从右截取指定长度的字串 
67 String.prototype.right = function(n) {
68     return this.slice(this.length - n);
69 }
70 // HTML编码 
71 String.prototype.HTMLEncode = function() {
72     var re = this;
73     var q1 = [ /x26/g, /x3C/g, /x3E/g, /x20/g ];
74     var q2 = [ "&", "<", ">", " " ];
75     for ( var i = 0; i < q1.length; i++)
76         re = re.replace(q1[i], q2[i]);
77     return re;
78 }
79 // Unicode转化 
80 String.prototype.ascW = function() {
81     var strText = "";
82     for ( var i = 0; i < this.length; i++)
83         strText += "&#" + this.charCodeAt(i) + ";";
84     return strText;
85 }

应用实例:

1 //页面显示
2 String.prototype.filterSpetialChar = function () {
3     
4     return this.replace(‘<‘, ‘&lt;‘).replace(‘>‘, ‘&gt;‘).replace(‘&‘, ‘&amp;‘).replace(‘"‘, ‘&quot;‘).replace(‘\‘‘, ‘&#39;‘);
5 }

引用:

1  "fnCreatedCell": function (nTd, sData, oData) {
2                         $(nTd).html(‘<span class=actionLink onclick=skipPage("codetscount","‘ + oData.GName.filterSpetialChar() + ‘","‘ + oData.GModel + ‘")>‘ + oData.CodeTsCount + ‘</span>‘);
3                     }

 

原生js的String类扩展

标签:

原文地址:http://www.cnblogs.com/xiaoerlang90/p/4643587.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!