js中String的常用扩展
生活随笔
收集整理的這篇文章主要介紹了
js中String的常用扩展
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
js中String的常用擴展,包括trim,檢查中文,url,emal,電話號碼,轉類型,格式化代碼等
//去掉字符串空間調用方式 字符串.trim() String.prototype.trim = function(){ return this.replace(/(^\s*)|(\s*$)/g, "");} //求字符穿真實長度漢字2個字節 字符串.lengthw() String.prototype.lengthW = function(){ return this.replace(/[^\x00-\xff]/g,"**").length;} //判斷是否email String.prototype.isEmail = function(){ return /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test(this);} // 字符串中是否包含中文 String.prototype.existChinese = function(){return /^[\x00-\xff]*$/.test(this);} //檢查url String.prototype.isUrl = function(){ return /^http[s]?:\/\/([\w-]+\.)+[\w-]+([\w-./?%&=]*)?$/i.test(this);} //檢查電話號碼 String.prototype.isPhoneCall = function(){ return /(^[0-9]{3,4}\-[0-9]{3,8}$)|(^[0-9]{3,8}$)|(^\([0-9]{3,4}\)[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$)/.test(this);} //檢查整數 String.prototype.isNumber=function(){return /^[0-9]+$/.test(this);} // 整數轉換 String.prototype.toNumber = function(def){return isNaN(parseInt(this, 10)) ? def : parseInt(this, 10);} // 小數轉換 String.prototype.toMoney = function(def){return isNaN(parseFloat(this)) ? def : parseFloat(this);} //格式化代碼 String.prototype.format = function() { var args = arguments; return this.replace(/{(\d{1})}/g, function() { return args[arguments[1]]; }); };轉載于:https://www.cnblogs.com/mccj/archive/2010/08/31/1813917.html
總結
以上是生活随笔為你收集整理的js中String的常用扩展的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: WinRAR最新版V3.93 破解方法
- 下一篇: vsftp本地用户,虚拟用户,匿名用户同