微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

最全正则表达式总结:验证QQ号、手机号、Email、中文、邮编、身份证、IP地址等

什么是 RegExp?

RegExp 是正则表达式(Regular expression)的缩写,作用是对字符串执行模式匹配。

通常用于格式验证、正则替换、查找子串等

各种编程语言的正则表达式基本相同,不同的语言可能会有一些细小的差别

RegExp 语法

1.// 直接实例化

2.var reg = new RegExp(pattern [,flags])

3.// 隐式创建(推荐)

4.var reg = /pattern/flags;

参数 pattern 是一个字符串,指定了正则表达式的模式或其他正则表达式。

参数 [,flags] 是一个可选的字符串,包含属性 “g”(global )、”i” (ignoreCase)和 “m”(multiline)。

ECMAScript 标准化之前,不支持 m 属性。如果 pattern 是正则表达式,而不是字符串,则必须省略该参数。

概念:子表达式

在正则表达式中,使用括号括起来的内容一个子表达式,子表达式匹配到的内容会被系统捕获至缓冲区,使用\n(n:数字)来反向引用系统的第n号缓冲区的内容

场景:后面的内容要求与前面的一致,可以使用子表达式

1.// 查找连续相同的四个数字

2.var str = "1212ab45677778cd";

3.var reg = /(\d)\1\1\1/gi;

4.console.log(str.match(reg));

5.// OUTPUT:7777

概念:方括号(字符簇)

1.var str = "Is this all there is?"; 2.var patt1 = /[a-h]/g; 3.document.write(str.match(patt1)); 4.// OUTPUT:h,a,h,e,e

方括号作用

Box-sizing: border-Box; font-size: 16px; max-width: 100%; font-family: "Helvetica Neue",Arial,"Hiragino Sans GB",STHeiti,"Microsoft YaHei","WenQuanYi Micro Hei",Simsun,Song,sans-serif; white-space: normal; border-collapse: collapse; text-transform: none; word-spacing: 0px; font-weight: normal; color: rgb(44,63,81); outline-width: medium !important; padding-bottom: 0px; font-style: normal; padding-top: 0px; outline-style: none !important; padding-left: 0px; margin: 0px 0px 20px; border-spacing: 0px; orphans: 2; widows: 1; letter-spacing: normal; outline-color: invert !important; line-height: 1.6; padding-right: 0px; width: 953px; background-color: transparent; text-indent: 0px; font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial"> Box-sizing: border-Box; outline-width: medium !important; padding-bottom: 0px; padding-top: 0px; outline-style: none !important; padding-left: 0px; margin: 0px; outline-color: invert !important; line-height: 1.6; padding-right: 0px"> Box-sizing: border-Box; outline-width: medium !important; padding-bottom: 0px; padding-top: 0px; outline-style: none !important; padding-left: 0px; margin: 0px; outline-color: invert !important; line-height: 1.6; padding-right: 0px"> Box-sizing: border-Box; outline-width: medium !important; padding-bottom: 0px; padding-top: 0px; outline-style: none !important; padding-left: 0px; margin: 0px; outline-color: invert !important; line-height: 1.6; padding-right: 0px"> Box-sizing: border-Box; outline-width: medium !important; padding-bottom: 0px; padding-top: 0px; outline-style: none !important; padding-left: 0px; margin: 0px; outline-color: invert !important; line-height: 1.6; padding-right: 0px">
Box-sizing: border-Box; border-left-style: solid; height: 40px; border-top-color: rgb(221,221,221); vertical-align: bottom; border-bottom-style: solid; font-weight: normal; outline-width: medium !important; padding-bottom: 0.5em; font-style: normal; border-bottom-color: rgb(221,221); text-align: left; padding-top: 0.5em; border-right-style: solid; outline-style: none !important; padding-left: 0.5em; margin: 0px; border-right-color: rgb(221,221); outline-color: invert !important; line-height: 1.5; padding-right: 0.5em; border-left-color: rgb(221,221)">方括号 Box-sizing: border-Box; border-left-style: solid; height: 40px; border-top-color: rgb(221,221)">作用
Box-sizing: border-Box; border-left-style: solid; height: 40px; border-top-color: rgb(221,221); vertical-align: top; border-bottom-style: solid; outline-width: medium !important; padding-bottom: 0.5em; border-bottom-color: rgb(221,221); text-align: center; padding-top: 0.5em; border-right-style: solid; outline-style: none !important; padding-left: 0.5em; margin: 0px; border-right-color: rgb(221,221); outline-color: invert !important; line-height: 1.6; padding-right: 0.5em; border-left-color: rgb(221,221)">[abc]Box-sizing: border-Box; border-left-style: solid; height: 40px; border-top-color: rgb(221,221)">查找方括号之间的任何字符。Box-sizing: border-Box; outline-width: medium !important; padding-bottom: 0px; padding-top: 0px; outline-style: none !important; padding-left: 0px; margin: 0px; outline-color: invert !important; line-height: 1.6; padding-right: 0px"> Box-sizing: border-Box; border-left-style: solid; height: 40px; border-top-color: rgb(221,221)">[^abc]Box-sizing: border-Box; border-left-style: solid; height: 40px; border-top-color: rgb(221,221)">查找任何不在方括号之间的字符。Box-sizing: border-Box; outline-width: medium !important; padding-bottom: 0px; padding-top: 0px; outline-style: none !important; padding-left: 0px; margin: 0px; outline-color: invert !important; line-height: 1.6; padding-right: 0px"> Box-sizing: border-Box; border-left-style: solid; height: 40px; border-top-color: rgb(221,221)">[0-9]Box-sizing: border-Box; border-left-style: solid; height: 40px; border-top-color: rgb(221,221)">查找任何从 0 至 9 的数字。同 \dBox-sizing: border-Box; outline-width: medium !important; padding-bottom: 0px; padding-top: 0px; outline-style: none !important; padding-left: 0px; margin: 0px; outline-color: invert !important; line-height: 1.6; padding-right: 0px"> Box-sizing: border-Box; border-left-style: solid; height: 40px; border-top-color: rgb(221,221)">[a-z]Box-sizing: border-Box; border-left-style: solid; height: 40px; border-top-color: rgb(221,221)">查找任何从小写 a 到小写 z 的字符。Box-sizing: border-Box; outline-width: medium !important; padding-bottom: 0px; padding-top: 0px; outline-style: none !important; padding-left: 0px; margin: 0px; outline-color: invert !important; line-height: 1.6; padding-right: 0px"> Box-sizing: border-Box; border-left-style: solid; height: 40px; border-top-color: rgb(221,221)">[A-Z]Box-sizing: border-Box; border-left-style: solid; height: 40px; border-top-color: rgb(221,221)">查找任何从大写 A 到大写 Z 的字符。Box-sizing: border-Box; outline-width: medium !important; padding-bottom: 0px; padding-top: 0px; outline-style: none !important; padding-left: 0px; margin: 0px; outline-color: invert !important; line-height: 1.6; padding-right: 0px"> Box-sizing: border-Box; border-left-style: solid; height: 40px; border-top-color: rgb(221,221)">[A-z]Box-sizing: border-Box; border-left-style: solid; height: 40px; border-top-color: rgb(221,221)">查找任何从大写 A 到小写 z 的字符。Box-sizing: border-Box; outline-width: medium !important; padding-bottom: 0px; padding-top: 0px; outline-style: none !important; padding-left: 0px; margin: 0px; outline-color: invert !important; line-height: 1.6; padding-right: 0px"> Box-sizing: border-Box; border-left-style: solid; height: 40px; border-top-color: rgb(221,221)">[0-9a-zA-Z]Box-sizing: border-Box; border-left-style: solid; height: 40px; border-top-color: rgb(221,221)">查找0-9,a-z,A-Z

概念:元字符

元字符(Metacharacter)是拥有特殊含义的字符:

元字符 | 作用 —|— \ | 转义符 (、)、/、\ | | 选择匹配符,可以匹配多个规则 . | 查找单个字符,除了换行和行结束符。 \w | 查找单词字符。字符 ( 字母 ,数字,下划线_ ) \W | 查找非单词字符。 \d | 查找数字。 \D | 查找非数字字符。 \s | 查找空白字符。空格 \S | 查找非空白字符。 \b | 匹配单词边界。 \B | 匹配非单词边界。 \0 | 查找 NUL 字符。 \n | 查找换行符。 \f | 查找换页符。 \r | 查找回车符。 \t | 查找制表符。 \v | 查找垂直制表符。 \xxx | 查找以八进制数 xxx 规定的字符。 \xdd | 查找以十六进制数 dd 规定的字符。 \uxxxx | 查找以十六进制数 xxxx 规定的 Unicode 字符。

概念:量词

Box-sizing: border-Box; font-size: 16px; max-width: 100%; font-family: "Helvetica Neue",221)">量词
Box-sizing: border-Box; border-left-style: solid; height: 40px; border-top-color: rgb(221,221)">n+Box-sizing: border-Box; border-left-style: solid; height: 40px; border-top-color: rgb(221,221)">匹配任何包含至少一个 n 的字符串。同 {1,}Box-sizing: border-Box; outline-width: medium !important; padding-bottom: 0px; padding-top: 0px; outline-style: none !important; padding-left: 0px; margin: 0px; outline-color: invert !important; line-height: 1.6; padding-right: 0px"> Box-sizing: border-Box; border-left-style: solid; height: 40px; border-top-color: rgb(221,221)">n*Box-sizing: border-Box; border-left-style: solid; height: 40px; border-top-color: rgb(221,221)">匹配任何包含零个或多个 n 的字符串。同 {0,221)">n?Box-sizing: border-Box; border-left-style: solid; height: 40px; border-top-color: rgb(221,221)">匹配任何包含零个或一个 n 的字符串。同 {0,1}Box-sizing: border-Box; outline-width: medium !important; padding-bottom: 0px; padding-top: 0px; outline-style: none !important; padding-left: 0px; margin: 0px; outline-color: invert !important; line-height: 1.6; padding-right: 0px"> Box-sizing: border-Box; border-left-style: solid; height: 40px; border-top-color: rgb(221,221)">n{X}Box-sizing: border-Box; border-left-style: solid; height: 40px; border-top-color: rgb(221,221)">匹配包含 X 个 n 的序列的字符串。Box-sizing: border-Box; outline-width: medium !important; padding-bottom: 0px; padding-top: 0px; outline-style: none !important; padding-left: 0px; margin: 0px; outline-color: invert !important; line-height: 1.6; padding-right: 0px"> Box-sizing: border-Box; border-left-style: solid; height: 40px; border-top-color: rgb(221,221)">n{X,Y}Box-sizing: border-Box; border-left-style: solid; height: 40px; border-top-color: rgb(221,221)">匹配包含 X 至 Y 个 n 的序列的字符串。Box-sizing: border-Box; outline-width: medium !important; padding-bottom: 0px; padding-top: 0px; outline-style: none !important; padding-left: 0px; margin: 0px; outline-color: invert !important; line-height: 1.6; padding-right: 0px"> Box-sizing: border-Box; border-left-style: solid; height: 40px; border-top-color: rgb(221,}Box-sizing: border-Box; border-left-style: solid; height: 40px; border-top-color: rgb(221,221)">匹配包含至少 X 个 n 的序列的字符串。Box-sizing: border-Box; outline-width: medium !important; padding-bottom: 0px; padding-top: 0px; outline-style: none !important; padding-left: 0px; margin: 0px; outline-color: invert !important; line-height: 1.6; padding-right: 0px"> Box-sizing: border-Box; border-left-style: solid; height: 40px; border-top-color: rgb(221,221)">n$Box-sizing: border-Box; border-left-style: solid; height: 40px; border-top-color: rgb(221,221)">匹配任何结尾为 n 的字符串。Box-sizing: border-Box; outline-width: medium !important; padding-bottom: 0px; padding-top: 0px; outline-style: none !important; padding-left: 0px; margin: 0px; outline-color: invert !important; line-height: 1.6; padding-right: 0px"> Box-sizing: border-Box; border-left-style: solid; height: 40px; border-top-color: rgb(221,221)">^nBox-sizing: border-Box; border-left-style: solid; height: 40px; border-top-color: rgb(221,221)">匹配任何开头为 n 的字符串。注意 /[^a] / 和 /^ [a]/是不一样的,前者是排除的,后者是代表首位。Box-sizing: border-Box; outline-width: medium !important; padding-bottom: 0px; padding-top: 0px; outline-style: none !important; padding-left: 0px; margin: 0px; outline-color: invert !important; line-height: 1.6; padding-right: 0px"> Box-sizing: border-Box; border-left-style: solid; height: 40px; border-top-color: rgb(221,221)">(?=n)Box-sizing: border-Box; border-left-style: solid; height: 40px; border-top-color: rgb(221,221)">匹配任何其后紧接指定字符串 n 的字符串。正向预查Box-sizing: border-Box; outline-width: medium !important; padding-bottom: 0px; padding-top: 0px; outline-style: none !important; padding-left: 0px; margin: 0px; outline-color: invert !important; line-height: 1.6; padding-right: 0px"> Box-sizing: border-Box; border-left-style: solid; height: 40px; border-top-color: rgb(221,221)">(?!n)Box-sizing: border-Box; border-left-style: solid; height: 40px; border-top-color: rgb(221,221)">匹配任何其后没有紧接指定字符串 n 的字符串。反向预查

RegExp 对象方法

test()

test() 方法检索字符串中是否存在指定的值。返回值是 true 或 false。

1.var patt1 = new RegExp('e'); 2.console.log(patt1.test('some text')); 3.// OUTPUT:true 4.var patt2 = new RegExp('ee'); 5.console.log(patt2.test('some text')); 6.// OUTPUT:false

1.// 判断是不是QQ号 2.// 1 首位不能是0 ^[1-9] 3.// 2 必须是 [5,11] 位的数字 \d{4,9} 4.var str = '80583600'; 5.var regexp = /^[1-9][0-9]{4,10}$/gim; 6.if (regexp.test(str)) { 7. alert('is'); 8.} else { 9. alert('no'); 10.}

exec()

exec() 方法检索字符串中的指定值。返回值是被找到的值。如果没有发现匹配,则返回 null。

1.var patt1 = new RegExp('e'); 2.console.log(patt1.exec('some text')); 3.// OUTPUT:e 4.var patt2 = new RegExp('ee'); 5.console.log(patt2.exec('some text')); 6.// OUTPUT:null

compile()

compile() 既可以改变检索模式,也可以添加删除第二个参数。

1.var patt1=new RegExp("e"); 2.document.write(patt1.test("The best things in life are free")); // true 3.// 改变了检索模式 4.patt1.compile("eee"); 5.document.write(patt1.test("The best things in life are free")); // false 支持正则表达式的 String 对象的方法

search 检索与正则表达式相匹配的值。

1.var str = "Visit W3School!" 2.console.log(str.search(/W3School/)) 3.// OUTPUT:6 match 找到一个或多个正则表达式的匹配。

1.var str="1 plus 2 equal 3" 2.console.log(str.match(/\d+/g)) 3.// OUTPUT:1,2,3 replace 替换与正则表达式匹配的子串。

1.var str = "Visit Microsoft!" 2.console.log(str.replace(/Microsoft/,"W3School")); 3.// OUTPUT:Visit W3School! 1.// 找重复项最多的字符个数 2.var str = 'g21ss4aeba_ersb43sgnnsssstht6sss60snnsj8resw0_ss'; 3.// split : 将字符串转化为数组 4.// sort : 对数组排序,ASCII 5.// join : 将数组转化为字符串 6.var str_new = str.split('').sort().join(''); 7.document.write(str + '
'); 8.document.write(str.split('') + '
'); 9.document.write(str.split('').sort() + '
'); 10.document.write(str.split('').sort().join('') + '
'); 11.// 匹配字符,且重复这个字符,重复次数至少一次。 12.var regexp = /(\w)\1+/g; 13.var index = 0; 14.var value = ''; 15.str_new.replace(regexp,function($0,$1) { 16. // document.write($0); 17. // document.write($1); 18. if (index < $0.length) { 19. index = $0.length; 20. value = $1; 21. } 22.}); 23.document.write('重复项最多的字符是:' + value + ',个数是:' + index); 24.// OUTPUT: 25.// 0012344668__aabbeeegghjnnnnrRSSssssssssssssssttw 26.// 重复项最多的字符是:s,个数是:16 split 把字符串分割为字符串数组。

1.var str = "How are you doing today?" 2.document.write(str.split(/\s+/)); 3.// OUTPUT:How,are,you,doing,today?

经验:

检验格式(邮箱格式、IP格式)是否正确,用test() 抓取星期(如所有手机号),用exec()、match() 替换敏感词汇,用replace()

常见的 正则表达式 校验

1.// 常见的 正则表达式 校验 2.// QQ号、手机号、Email、是否是数字、去掉前后空格、是否存在中文、邮编、身份证、URL、日期格式、IP 3.var myRegExp = { 4. // 检查字符串是否为合法QQ号码 5. isQQ: function(str) { 6. // 1 首位不能是0 ^[1-9] 7. // 2 必须是 [5,9} 8. var reg = /^[1-9][0-9]{4,9}$/gim; 9. if (reg.test(str)) { 10. console.log('QQ号码格式输入正确'); 11. return true; 12. } else { 13. console.log('请输入正确格式的QQ号码'); 14. return false; 15. } 16. }, 17. // 检查字符串是否为合法手机号码 18. isPhone: function(str) { 19. var reg = /^(0|86|17951)?(13[0-9]|15[012356789]|18[0-9]|14[57]|17[678])[0-9]{8}$/; 20. if (reg.test(str)) { 21. console.log('手机号码格式输入正确'); 22. return true; 23. } else { 24. console.log('请输入正确格式的手机号码'); 25. return false; 26. } 27. }, 28. // 检查字符串是否为合法Email地址 29. isEmail: function(str) { 30. var reg = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/; 31. // var reg = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/; 32. if (reg.test(str)) { 33. console.log('Email格式输入正确'); 34. return true; 35. } else { 36. console.log('请输入正确格式的Email'); 37. return false; 38. } 39. }, 40. // 检查字符串是否是数字 41. isNumber: function(str) { 42. var reg = /^\d+$/; 43. if (reg.test(str)) { 44. console.log(str + '是数字'); 45. return true; 46. } else { 47. console.log(str + '不是数字'); 48. return false; 49. } 50. }, 51. // 去掉前后空格 52. trim: function(str) { 53. var reg = /^\s+|\s+$/g; 54. return str.replace(reg,''); 55. }, 56. // 检查字符串是否存在中文 57. isChinese: function(str) { 58. var reg = /[\u4e00-\u9fa5]/gm; 59. if (reg.test(str)) { 60. console.log(str + ' 中存在中文'); 61. return true; 62. } else { 63. console.log(str + ' 中不存在中文'); 64. return false; 65. } 66. }, 67. // 检查字符串是否为合法邮政编码 68. isPostcode: function(str) { 69. // 起始数字不能为0,然后是5个数字 [1-9]\d{5} 70. var reg = /^[1-9]\d{5}$/g; 71. // var reg = /^[1-9]\d{5}(?!\d)$/; 72. if (reg.test(str)) { 73. console.log(str + ' 是合法的邮编格式'); 74. return true; 75. } else { 76. console.log(str + ' 是不合法的邮编格式'); 77. return false; 78. } 79. }, 80. // 检查字符串是否为合法身份证号码 81. isIDcard: function(str) { 82. var reg = /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/; 83. if (reg.test(str)) { 84. console.log(str + ' 是合法的身份证号码'); 85. return true; 86. } else { 87. console.log(str + ' 是不合法的身份证号码'); 88. return false; 89. } 90. }, 91. // 检查字符串是否为合法URL 92. isURL: function(str) { 93. var reg = /^https?:\/\/(([a-zA-Z0-9_-])+(\.)?)*(:\d+)?(\/((\.)?(\?)?=?&?[a-zA-Z0-9_-](\?)?)*)*$/i; 94. if (reg.test(str)) { 95. console.log(str + ' 是合法的URL'); 96. return true; 97. } else { 98. console.log(str + ' 是不合法的URL'); 99. return false; 100. } 101. }, 102. // 检查字符串是否为合法日期格式 yyyy-mm-dd 103. isDate: function(str) { 104. var reg = /^[1-2][0-9][0-9][0-9]-[0-1]{0,1}[0-9]-[0-3]{0,1}[0-9]$/; 105. if (reg.test(str)) { 106. console.log(str + ' 是合法的日期格式'); 107. return true; 108. } else { 109. console.log(str + ' 是不合法的日期格式,yyyy-mm-dd'); 110. return false; 111. } 112. }, 113. // 检查字符串是否为合法IP地址 114. isIP: function(str) { 115. // 1.1.1.1 四段 [0,255] 116. // 第一段不能为0 117. // 每个段不能以0开头 118. // 119. // 本机IP: 58.50.120.18 湖北省荆州市 电信 120. var reg = /^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$/gi; 121. if (reg.test(str)) { 122. console.log(str + ' 是合法的IP地址'); 123. return true; 124. } else { 125. console.log(str + ' 是不合法的IP地址'); 126. return false; 127. } 128. } 129.} 130.// 测试 131.// console.log(myRegExp.isQQ('80583600')); 132.// console.log(myRegExp.isPhone('17607160722')); 133.// console.log(myRegExp.isEmail(nofollow" href="mailto:'80583600@qq.com'">'80583600@qq.com')); 134.// console.log(myRegExp.isNumber('100a')); 135.// console.log(myRegExp.trim(' 100 ')); 136.// console.log(myRegExp.isChinese('baixiaoming')); 137.// console.log(myRegExp.isChinese('小明')); 138.// console.log(myRegExp.isPostcode('412345')); 139.// console.log(myRegExp.isIDcard('42091119940927001X')); 140.// console.log(myRegExp.isURL('https://www.baidu.com/')); 141.// console.log(myRegExp.isDate('2017-4-4')); 142.// console.log(myRegExp.isIP('1.0.0.0'));

一、校验数字的表达式

1.数字:^[0-9]*$ 2.n位的数字:^\d{n}$ 3.至少n位的数字:^\d{n,}$ 4.m-n位的数字:^\d{m,n}$ 5.零和非零开头的数字:^(0|[1-9][0-9]*)$ 6.非零开头的最多带两位小数的数字:^([1-9][0-9]*)+(.[0-9]{1,2})?$ 7.带1-2位小数的正数或负数:^(\-)?\d+(\.\d{1,2})?$ 8.正数、负数、和小数:^(\-|\+)?\d+(\.\d+)?$ 9.有两位小数的正实数:^[0-9]+(.[0-9]{2})?$ 10.有1~3位小数的正实数:^[0-9]+(.[0-9]{1,3})?$ 11.非零的正整数:^[1-9]\d*$ 或 ^([1-9][0-9]*){1,3}$ 或 ^\+?[1-9][0-9]*$ 12.非零的负整数:^\-[1-9][]0-9"*$ 或 ^-[1-9]\d*$ 13.非负整数:^\d+$ 或 ^[1-9]\d*|0$ 14.非正整数:^-[1-9]\d*|0$ 或 ^((-\d+)|(0+))$ 15.非负浮点数:^\d+(\.\d+)?$ 或 ^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$ 16.非正浮点数:^((-\d+(\.\d+)?)|(0+(\.0+)?))$ 或 ^(-([1-9]\d*\.\d*|0\.\d*[1-9]\d*))|0?\.0+|0$ 17.正浮点数:^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$ 或 ^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$ 18.负浮点数:^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)$ 或 ^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$ 19.浮点数:^(-?\d+)(\.\d+)?$ 或 ^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$

二、校验字符的表达式

1.汉字:^[\u4e00-\u9fa5]{0,}$ 2.英文和数字:^[A-Za-z0-9]+$ 或 ^[A-Za-z0-9]{4,40}$ 3.长度为3-20的所有字符:^.{3,20}$ 4.由26个英文字母组成的字符串:^[A-Za-z]+$ 5.由26个大写英文字母组成的字符串:^[A-Z]+$ 6.由26个小写英文字母组成的字符串:^[a-z]+$ 7.由数字和26个英文字母组成的字符串:^[A-Za-z0-9]+$ 8.由数字、26个英文字母或者下划线组成的字符串:^\w+$ 或 ^\w{3,20}$ 9.中文、英文、数字包括下划线:^[\u4E00-\u9FA5A-Za-z0-9_]+$ 10.可以输入含有^%&',;=?$\"等字符:[^%&',;=?$\x22]+ 11.禁止输入含有~的字符:[^~\x22]+

三、特殊需求表达式

1.Email地址:^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$ 2.域名:[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(/.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+/.? 3.InternetURL:[a-zA-z]+://[^\s]* 或 ^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$ 4.手机号码:^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$ 5.电话号码("XXX-XXXXXXX"、"XXXX-XXXXXXXX"、"XXX-XXXXXXX"、"XXX-XXXXXXXX"、"XXXXXXX"和"XXXXXXXX):^(\(\d{3,4}-)|\d{3.4}-)?\d{7,8}$ 6.国内电话号码(0511-4405222、021-87888822):\d{3}-\d{8}|\d{4}-\d{7} 7.身份证号(15位、18位数字):^\d{15}|\d{18}$ 8.短身份证号码(数字、字母x结尾):^([0-9]){7,18}(x|X)?$ 或 ^\d{8,18}|[0-9x]{8,18}|[0-9X]{8,18}?$ 9.帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线):^[a-zA-Z][a-zA-Z0-9_]{4,15}$ 10.密码(以字母开头,长度在6~18之间,只能包含字母、数字和下划线):^[a-zA-Z]\w{5,17}$ 11.强密码(必须包含大小写字母和数字的组合,不能使用特殊字符,长度在8-10之间):^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$ 12.日期格式:^\d{4}-\d{1,2}-\d{1,2} 13.一年的12个月(01~09和1~12):^(0?[1-9]|1[0-2])$ 14.一个月的31天(01~09和1~31):^((0?[1-9])|((1|2)[0-9])|30|31)$ 15.xml文件:^([a-zA-Z]+-?)+[a-zA-Z0-9]+\\.[x|X][m|M][l|L]$ 16.中文字符的正则表达式:[\u4e00-\u9fa5] 17.双字节字符:[^\x00-\xff] (包括汉字在内,可以用来计算字符串的长度(一个双字节字符长度计2,ASCII字符计1)) 18.空白行的正则表达式:\n\s*\r (可以用来删除空白行) 19.HTML标记的正则表达式:<(\S*?)[^>]*>.*?|<.*? /> (网上流传的版本太糟糕,上面这个也仅仅能部分,对于复杂的嵌套标记依旧无能为力) 20.首尾空白字符的正则表达式:^\s*|\s*$或(^\s*)|(\s*$) (可以用来删除行首行尾的空白字符(包括空格、制表符、换页符等等),非常有用的表达式) 21.腾讯QQ号:[1-9][0-9]{4,} (腾讯QQ号从10000开始) 22.中国邮政编码:[1-9]\d{5}(?!\d) (中国邮政编码为6位数字) 23.IP地址:\d+\.\d+\.\d+\.\d+ (提取IP地址时有用) 24.IP地址:((?:(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d)\\.){3}(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d))

钱的输入格式:

1.1.有四种钱的表示形式我们可以接受:"10000.00" 和 "10,000.00",和没有 "分" 的 "10000" 和 "10,000":^[1-9][0-9]*$ 2.2.这表示任意一个不以0开头的数字,但是,这也意味着一个字符"0"不通过,所以我们采用下面的形式:^(0|[1-9][0-9]*)$ 3.3.一个0或者一个不以0开头的数字.我们还可以允许开头有一个负号:^(0|-?[1-9][0-9]*)$ 4.4.这表示一个0或者一个可能为负的开头不为0的数字.让用户以0开头好了.把负号的也去掉,因为钱总不能是负的吧.下面我们要加的是说明可能的小数部分:^[0-9]+(.[0-9]+)?$ 5.5.必须说明的是,小数点后面至少应该有1位数,所以"10."是不通过的,但是 "10" 和 "10.2" 是通过的:^[0-9]+(.[0-9]{2})?$ 6.6.这样我们规定小数点后面必须有两位,如果你认为太苛刻了,可以这样:^[0-9]+(.[0-9]{1,2})?$ 7.7.这样就允许用户只写一位小数.下面我们该考虑数字中的逗号了,我们可以这样:^[0-9]{1,3}(,[0-9]{3})*(.[0-9]{1,2})?$ 8.8.1到3个数字,后面跟着任意个 逗号+3个数字,逗号成为可选,而不是必须:^([0-9]+|[0-9]{1,[0-9]{3})*)(.[0-9]{1,2})?$ 9.备注:这就是最终结果了,别忘了"+"可以用"*"替代如果你觉得空字符串也可以接受的话(奇怪,为什么?)最后,别忘了在用函数时去掉去掉那个反斜杠,一般的错误在这里

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助~如果有疑问大家可以留言交流,谢谢大家对编程之家的支持!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐