JavaScript字符处理操作

//过滤特殊字符
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"),结果:aa1,bb2
String.prototype.Format =function() { var args = arguments; return this.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); };

//将a或g用空代替
String.prototype.ResetBlank = function(){return this.replace(/a+/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, ""); }
//获取指定的值"5"
String.prototype.GetNum = function(){return this.replace(/[^5]/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 */
//从左边开始,获取2个字符
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 */

//将特殊字符用htmlCode代替,如:da5>,结果:da5&lt
String.prototype.HtmlEncode = function(){var i,e ={ '&': '&', '<': '<', '>': '>', '"': '"' },t = this; for (i in e) t = t.replace(new RegExp(i, 'g'), e[i]);return t}

//将URL中的要传递的字符转码如:"http://www.baidu.com?"+"中国".UrlEncode()+"&pwd=123"
String.prototype.UrlEncode = function(){return encodeURIComponent(this); }

//将字符串转换成Unicode
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 --*/
//往数组中增加元素,itAdd代表元素
Array.prototype.Add = function(itAdd) {
this.push(itAdd);
};

//删除指定下标的元素,n代表下标
Array.prototype.Del = function(n) {
if (n >= 0) {
this.splice(n, 1);
}};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值