Javascript 对象扩展积累

原文:http://www.cnblogs.com/DanielChow/archive/2009/12/14/1624133.html

 

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
/*2012-8-7 最后更新*/
//过滤特殊字符 
String.prototype.filterSpecialChar = function () {var pattern = new RegExp("[`~!@@#$^&*()=\\-+|{}':;',\\[\\]\\.%<>/?~!#¥……&*()—|{}【】‘;:”“'。,、?]");var rs = "";for (var i = 0; i < this.length; i++) {rs = rs + this.substr(i, 1).replace(pattern, '');} return rs;};
//eg."asdaisdias^&%&^$&$^#".filterSpecialChar()
String.prototype.ToCharArray = function(){return this.split("");}
String.prototype.Reverse = function(){return this.split("").reverse().join("");}
String.prototype.IsContains = function(str) { return (this.indexOf(str) > -1); } /* include or not ?*/ 
//格式化"aa{0},bb{1}".Format("1","2")
String.prototype.Format =function() { var args = arguments; returnthis.replace(/\{(\d+)\}/g, function(m, i, o, n) { return args[i]; }); };
//eg.alert('<a href="{clickurl}" target="_blank">{inner}</a>'.JsonFormat({clickurl:'http://www.baidu.com',inner:'baidu'}));
String.prototype.JsonFormat =function (config,reserve) {return this.replace(/\{([^}]*)\}/g,(typeof config=='object')?function (m,i) {var ret=config[i];return ret==null&&reserve?m:ret}:config); }; /*2010-1-3*/
String.prototype.ResetBlank = function(){return this.replace(/s+/g,"");}
String.prototype.LTrim = function(){return this.replace(/^s+/g, "");}
String.prototype.RTrim = function(){return this.replace(/s+$/g, "");}
String.prototype.Trim = function(){return this.replace(/(^\s*)|(\s*$)/g, ""); }
String.prototype.GetNum = function(){return this.replace(/[^d]/g, "");} /*num only*/
String.prototype.GetEn = function(){return this.replace(/[^A-Za-z]/g, ""); } /* english charctar only */
String.prototype.GetCn = function(){return this.replace(/[^\u4e00-\u9fa5\uf900-\ufa2d]/g, ""); } /* chinese charctar only */
String.prototype.ByteLength = function(){return this.replace(/[^\x00-\xff]/g, "aa").length; } /* get Byte Length */
String.prototype.Left = function(n){return this.slice(0, n);}
String.prototype.Right = function(n){return this.slice(this.length - n);}
String.prototype.Insert = function(index, str) { return this.substring(0, index) + str + this.substr(index);}
String.prototype.Copy = function(){if(IE) window.clipboardData.setData("text", this.toString()); }/* ie only */
String.prototype.HtmlEncode = function(){var i,e ={ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;' },t = this; for (i in e) t = t.replace(new RegExp(i, 'g'), e[i]);return t}
String.prototype.UrlEncode = function(){return encodeURIComponent(this); }
String.prototype.Unicode = function(){var tmpArr = []; for (var i = 0; i < this.length; i++) tmpArr.push("&#" + this.charCodeAt(i) + ";"); return tmpArr.join("");}
/*Validate*/
String.prototype.IsEmpty = function() { return this == ""; }
String.prototype.IsEmail = function(){return /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test(this);}
String.prototype.IsChinese = function(){return /^[\u0391-\uFFE5]+$/.test(this);}
String.prototype.IsQQ = function(){return /^[0-9]{5,9}$/.test(this);}
String.prototype.IsTel = function(){return /^(\(\d{3,4}\)|\d{3,4}-)?\d{7,8}$/.test(this);}
String.prototype.IsTelAll = function(){return /^(\(\d{3,4}\)|\d{3,4}-)?\d{7,8}$/.test(this) || /^\d{11}$/.test(this);} /* include cell phone */
String.prototype.IsNum = function() { return /^(\d+)$/.test(this); }
/*-- Array Class Extendtions --*/
Array.prototype.Add = function(itAdd) { var _index = this.indexOf(itAdd); if (_index < 0) {this.push(itAdd);}};
Array.prototype.Del = function(itDel) { var _index = this.indexOf(itDel); if (_index >= 0) { this.splice(_index, 1); }};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值