javascript字符串函数汇总(3)

String.prototype.isNumeric = function(flag) { //验证是否是数字 if(isNaN(this)) { return false; } switch(flag) { case null: //数字 case "": return true; case "+": //正数 return /(^\+?|^\d?)\d*\.?\d+$/.test(this); case "-": //负数 return /^-\d*\.?\d+$/.test(this); case "i": //整数 return /(^-?|^\+?|\d)\d+$/.test(this); case "+i": //正整数 return /(^\d+$)|(^\+?\d+$)/.test(this); case "-i": //负整数 return /^[-]\d+$/.test(this); case "f": //浮点数 return /(^-?|^\+?|^\d?)\d*\.\d+$/.test(this); case "+f": //正浮点数 return /(^\+?|^\d?)\d*\.\d+$/.test(this); case "-f": //负浮点数 return /^[-]\d*\.\d$/.test(this); default: //缺省 return true; } }

/*
===========================================
//是否是颜色(#FFFFFF形式)
===========================================
*/

String.prototype.IsColor = function() { var temp = this; if (temp=="") return true; if (temp.length!=7) return false; return (temp.search(/\#[a-fA-F0-9]{6}/) != -1); }

/*
===========================================
//转换成全角
===========================================
*/

String.prototype.toCase = function() { var tmp = ""; for(var i=0;i<this.length;i++) { if(this.charCodeAt(i)>0&&this.charCodeAt(i)<255) { tmp += String.fromCharCode(this.charCodeAt(i)+65248); } else { tmp += String.fromCharCode(this.charCodeAt(i)); } } return tmp }

/*
===========================================
//对字符串进行Html编码
===========================================
*/

String.prototype.toHtmlEncode = function() { var str = this; str=str.replace(/&/g,"&amp;"); str=str.replace(/</g,"&lt;"); str=str.replace(/>/g,"&gt;"); str=str.replace(/\'/g,"&apos;"); str=str.replace(/\"/g,"&quot;"); str=str.replace(/\n/g,"<br>"); str=str.replace(/\ /g,"&nbsp;"); str=str.replace(/\t/g,"&nbsp;&nbsp;&nbsp;&nbsp;"); return str; }

/*
===========================================
//转换成日期
===========================================
*/

String.prototype.toDate = function() { try { return new Date(this.replace(/-/g, "\/")); } catch(e) { return null; } }

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wgzwfz.html