IT技术博客大学习 共学习 共进步
全部 移动开发 后端 数据库 AI 算法 安全 DevOps 前端 设计 开发者

js中String的常用扩展

koyoz's blog 2009-11-15 18:24:20 累计浏览 2,194 次
本机暂存

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. Why Isn’t My 3D View Transition Working? (2026-06-12 20:53:41)
  2. 全新的CSS border-shape属性简介 (2026-06-10 11:00:06)
  3. Scroll-Driven, Scroll-Triggered, Scroll States, and View Transitions (2026-06-08 21:00:34)

查看更多 前端 文章 →

建议继续学习

  1. JQuery实现Excel表格呈现 (累计阅读 48,289)
  2. vim几个小技巧(批量替换,列编辑) (累计阅读 37,405)
  3. 深入理解Javascript之执行上下文(Execution Context) (累计阅读 18,306)
  4. 从输入 URL 到页面加载完成的过程中都发生了什么事情? (累计阅读 15,844)
  5. 图片动态局部毛玻璃模糊效果的实现 (累计阅读 14,788)
  6. 天朝第二代身份证号码的验证机制 (累计阅读 14,708)
  7. HTML 5 的data-* 自定义属性 (累计阅读 14,278)
  8. 分享一个JQUERY颜色选择插件 (累计阅读 14,169)
  9. 什么是全栈工程师? (累计阅读 13,974)
  10. 快速排序(Quicksort)的Javascript实现 (累计阅读 11,658)