技术头条 - 一个快速在微博传播文章的方式     搜索本站
您现在的位置首页 --> JavaScript --> js中String的常用扩展

js中String的常用扩展

浏览:1214次  出处信息

js中String的常用扩展,包括trim,检查中文,url,emal,电话号码,转类型等

JavaScript代码
  1. //去掉字符串空间调用方式 字符串.trim()   
  2. String.prototype.trim = function(){ return this.replace(/(^\s*)|(\s*$)/g, "");}       
  3.   
  4. //求字符穿真实长度汉字2个字节 字符串.lengthw()   
  5. String.prototype.lengthW = function(){ return this.replace(/[^\x00-\xff]/g,"**").length;}   
  6.   
  7. //判断是否email   
  8. String.prototype.isEmail = function(){ return /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test(this);}    
  9.   
  10. // 字符串中是否包含中文   
  11. String.prototype.existChinese = function(){return /^[\x00-\xff]*$/.test(this);}      
  12.   
  13. //检查url      
  14. String.prototype.isUrl = function(){ return /^http[s]?:\/\/([\w-]+\.)+[\w-]+([\w-./?%&=]*)?$/i.test(this);}   
  15.   
  16. //检查电话号码   
  17. 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);}    
  18.   
  19. //检查整数   
  20. String.prototype.isNumber=function(){return /^[0-9]+$/.test(this);}   
  21.   
  22. // 整数转换   
  23. String.prototype.toNumber = function(def){return isNaN(parseInt(this, 10)) ? def : parseInt(this, 10);}      
  24.   
  25. // 小数转换   
  26. String.prototype.toMoney = function(def){return isNaN(parseFloat(this)) ? def : parseFloat(this);}  

建议继续学习:

  1. JQuery,Extjs,YUI,Prototype,Dojo 等JS框架的区别和应用场景简述    (阅读:2915)
QQ技术交流群:445447336,欢迎加入!
扫一扫订阅我的微信号:IT技术博客大学习
© 2009 - 2024 by blogread.cn 微博:@IT技术博客大学习

京ICP备15002552号-1