以下是收集的一些大家经常用到的正则表达式整理,方便日后在工作中参考,特此记录一下。
只能输入数字:"^[0-9]*$"
只能输入 n 位的数字:"^\d{n}$"
只能输入至少 n 位的数字:"^\d{n,}$"
只能输入 m ~ n 位的数字:"^\d{m,n}$"
只能输入零和非零开头的数字:"^(0[1-9][0-9]*)$"
只能输入有两位小数的正实数:"^[0-9]+(.[0-9]{2})?$"
只能输入有 1~3 位小数的正实数:"^[0-9]+(.[0-9]{1,3})?$"
只能输入非零的正整数:"^\+?[1-9][0-9]*$"
只能输入非零的负整数:"^\-[1-9][0-9]*$"
只能输入长度位 3 的字符:"^.{3}$"
只能输入由 26 个英文字母组成的字符串:"^[A-Za-z]+$"
只能输入由 26 个大写英文字母组成的字符串:"^[A-Z]+$"
只能输入由 26 个小写英文字母组成的字符串:"^[a-z]+$"
只能输入由数字和 26 个英文字母组成的字符串:"^[A-Za-z0-9]+$"
只能输入由数字、26个英文字母或者下划线组成的字符串:"^\w+$"
验证用户密码: "^[a-zA-Z]\w{5,17}$" 正确格式位:以字母开头,长度在6~18之间,只能包含字符、数字和下划线
验证是否含有非法字符 ^(?:[\u4e00-\u9fa5]*\w*\s*)+$
只能输入汉字:"^[\u4e00-\u9fa5]{0,}$"
验证 Email 地址:"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"
验证 InternetURL:"^http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?$"
中国电话号码验证
匹配形式如:0511-4405222 或者021-87888822 或者 021-44055520-555 或者 (0511)4405222
正则表达式 "((d{3,4})d{3,4}-)?d{7,8}(-d{3})*"
中国邮政编码验证
匹配形式如:215421
正则表达式 "d{6}"
电子邮件验证
匹配形式如:justali@justdn.com
正则表达式 "w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*"
身份证验证
匹配形式如:15位或者18位身份证
正则表达式 "d{18}d{15}"
常用数字验证
正则表达式
"d{n}" n为规定长度
"d{n,m}" n到m的长度范围
非法字符验证
匹配排除非法字符的字符如:
正则表达式 ^(?:[\u4e00-\u9fa5]*\w*\s*)+$
日期验证
匹配形式如:20030718,030718
范围:1900--2099
正则表达式((((19){1}(20){1})d{2})d{2})[01]{1}d{1}[0-3]{1}d{1}
使用实例:
查看代码打印
01 <html> 02 <head> 03 <title>php正则表达式集锦实例</title> 04 <script language="javascript" type="text/javascript"> 05 function Button1_onclick(){ 06 if(!test_hanzi(document.form1.Text1.value)){ 07 alert("姓名只能是汉字"); 08 return false; 09 } 10 return true; 11 } 12 //验证电子邮件 13 function test_email(strEmail){ 14 var myReg = /^[_a-z0-9]+@([_a-z0-9]+\.)+[a-z0-9]{2,3}$/; 15 if(myReg.test(strEmail)) return true; 16 return false; 17 } 18 //只能输入数字 19 function test_shuzi(strshuzi){ 20 var myReg =/^[0-9]*$/; 21 if(myReg.test(strshuzi)) return true; 22 return false; 23 } 24 //只能输入汉字 25 function test_hanzi(strhanzi){ 26 var myReg =/^[\u4e00-\u9fa5]{0,}$/; 27 if(myReg.test(strhanzi)) return true; 28 return false; 29 } 30 </script> 31 </head> 32 <body> 33 <form name="form1">姓名: 34 <input id="Text1" name="Text1" type="text" /> <span style="font-family: Arial, Helvetica, sans-serif;">(PS:^_^不错的php开发交流群:304224365,验证:csl)</span> 35 <input name="按钮" type="button" id="Button1" onclick="return Button1_onclick()" value="button" language="javascript" /> 36 </form> 37 </body> 38 </html>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Notepad++7.3.1
Easy-to-use and free code editor
