常用的正则表达式
^从行开始处匹配,$从行结束处开始匹配。
\A从字符串开始处匹配,\Z从字符串结束处匹配。
1.匹配十六进制的字符串
// For C-style hex notation (0xFF) you can use @"\A\b(0[xX])?[0-9a-fA-F]+\b\Z"
@"\A\b[0-9a-fA-F]+\b\Z"
@"^[0-9a-fA-F]$"
关于\A和\b的,还要抽时间做实验
正则邮箱regex for email
How to validate an email address in JavaScript
Using regular expressions is probably the best way. You can see a bunch of tests here (taken from chromium)
function validateEmail(email) {
const re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(String(email).toLowerCase());
}
Here's the example of regular expresion that accepts unicode:
const re = /^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;
But keep in mind that one should not rely only upon JavaScript validation. JavaScript can easily be disabled. This should be validated on the server side as well.
Here's an example of the above in action:
function validateEmail(email) { const re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; return re.test(email); } function validate() { const $result = $("#result"); const email = $("#email").val(); $result.text(""); if (validateEmail(email)) { $result.text(email + " is valid :)"); $result.css("color", "green"); } else { $result.text(email + " is not valid :("); $result.css("color", "red"); } return false; } $("#email").on("input", validate);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <label for=email>Enter an email address:</label> <input id="email"> <h2 id="result"></h2>
另外一个答案,枚举了很多正则
How to write Particular Regular Expression in android or java.
1) USER_NAME = "^[A-Za-z0-9_-]{min number of character,max number of character}$";
2) TELEPHONE = "(^\\+)?[0-9()-]*";
3) TELEPHONE_OPTIONAL = "^($|(^\\+)?[0-9()-]*)$";
4) EMAIL = "[a-zA-Z0-9_\\.\\+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-\\.]+";
5) EMAIL_OPTIONAL = "^($|[a-zA-Z0-9_\\.\\+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-\\.]+)$";
6) WEB_URL = "^($|(http:\\/\\/|https:\\/\\/)?(www.)?([a-zA-Z0-9]+).[a-zA-Z0-9]*.[a-z]{3}.?([a-z]+)?)$";
7) WEB_URL_YOUTUBE_BE = "https?\\:\\/\\/(www\\.)?youtu(\\.)?be(\\.com)?\\/.*(\\?v=|\\/v\\/)?[a-zA-Z0-9_\\-]+";
8) POSTAL_ADDRESS = "[a-zA-Z\\d\\s\\-\\,\\#\\.\\+]+";
9) FIELD_NOT_EMPTY = "[^\\s]*";
10) PINCODE = "^([0-9]{6})?$";
11) IFSC_CODE = "^[^\\s]{4}\\d{7}$";
12) SWIFT_CODE = "^([0-9]{10})?$";
13) PINCODE = "^([0-9]{6})?$";
Invalid Characters in Internet Email Addresses
Internet email addresses must include only RFC-compliant characters, which include:
-
Numbers 0-9
-
Uppercase letters A-Z
-
Lowercase letters a-z
-
Plus sign +
-
Hyphen -
-
Underscore _
-
Tilde ~
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2016-05-17 Get Length 使用dynamic关键字
2015-05-17 对象池的实际应用