当前位置:网站首页>分享 12 个最常用的正则表达式,能解决你大部分问题
分享 12 个最常用的正则表达式,能解决你大部分问题
2022-07-05 14:09:00 【前端达人】

英文 | https://medium.com/frontend-canteen/you-dont-need-to-fully-understand-regex-you-just-need-to-know-these-10-most-used-expressions-197dd2397308
翻译 | 杨小爱
坦率地说,我从来没有理解过正则表达式,我花了很多时间学习它,但我仍然不明白。
后来我明白了一个道理:我真的不需要完全理解正则表达式的所有原理,把一些常用的正则表达式理解通透,因为我们真正使用的正则表达式数量有限,我只需将它们添加到我的笔记本中(甚至不需要记住)。
今天,我收集整理了一些web项目中经常用到的正则表达式,我觉得可以解决50%以上的正则表达式问题。
数字的
01、仅匹配数字字符串
目标:
12312
1232121
4353正则表达式:
/^\d+$/简单解释:
^ 表示字符串的开头
\d+ 匹配一位或多位数字
$ 表示字符串的结尾
可视化:

用法:

234,124 包含非法字符 , ,因此它返回 false。
id12313 包含两个非法字符 id ,因此它返回 false。
02、十进制数
目标:
123.123
11
3.14
0.43
0
66
123正则表达式:
/^\d+(\.?\d+)?$/\.匹配单个 char 。
? 表示可选。
() 表示一个组
(\.?\d)? 是一个可选组
可视化:

用法:

. 应该在数字中间,所以 .1 和 12.返回 false。
03、字母数字字符
我们在检测用户名和密码时经常使用这个正则表达式。
目标:
123
abc
123abc
acRa32EEE
bytefish
Jack2022正则表达式:
/^[a-zA-Z0-9]+$/[a-zA-Z0–9] 匹配所有字母和数字
可视化:

用法:

04、allow space
如果你想在字符串中留出空格,你可以像这样写正则表达式:
/^[a-zA-Z0-9\s]+$/\s 匹配空白。

用法:

05、电子邮件
电子邮件地址可能有多种格式,我们很难写出完美匹配电子邮件地址的正则表达式。
如果我们添加一些约束,那么我们有以下写法。
正则表达式1
^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$这是 RegularExpressionValidator 在 ASP.NET 中使用的一种。

用法:

正则表达式2
^\[email protected][a-zA-Z_]+?\.[a-zA-Z]{2,3}$简单的电子邮件表达。不允许在域名中使用数字,并且不允许使用少于 2 个或多于 3 个字母的顶级域(在它们允许更多之前都可以)。

用法:

06、密码
与电子邮件一样,密码可能有不同的规则。下面是一些常见的规则和相关的正则表达式。
规则1
^[a-zA-Z]\w{8,20}$在这个正则表达式中,密码的第一个字符必须是字母,它必须包含至少 8 个字符且不超过 20 个字符,并且不能使用除字母、数字和下划线以外的字符。

用法:

规则2:
/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[^\w\s]).{8,}$/至少 8 个字符
至少 1 个数字字符
至少 1 个小写字母
至少 1 个大写字母
至少 1 个特殊字符

07、用户名
可能包含 _ 和 — 的字母数字字符串,长度为 3 到 16 个字符。
例子:
bytefish
jon-snow
Rob_Stark正则表达式:
/^[a-zA-Z0-9_-]{3,16}$/
用法:

08、网址
检查字符串是否为 URL
/https?:\/\/(www\.)?[[email protected]:%._\+~#=]{2,256}\.[a-z]{2,6}\b([[email protected]:%_\+.~#()?&//=]*)/
用法:

09、IP地址
IPv4
^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$

IPv6
(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))10、日期
使用分隔符的日期格式 YYYY-MM-dd -
/([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))/
用法:

日期格式 dd-MM-YYYY 使用分隔符 - 或 . /
/^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$/

11、HTML 标签
将 HTML 标记与属性匹配:
/<\/?[\w\s]*>|<.+[\W]>/
用法:

12、电话号码
美国电话号码格式
目标:
123-456-7890
(123) 456-7890
123 456 7890
123.456.7890
+91 (123) 456-7890正则表达式:
^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$
用法:

总结
以上就是我今天跟大家分享的12个常用的正则表达式,希望这些正则表达式对你有用。
最后,感谢你的阅读。
学习更多技能
请点击下方公众号
![]()
边栏推荐
- 昆仑太科冲刺科创板:年营收1.3亿拟募资5亿 电科太极持股40%
- Shen Ziyu, nouveau Président de Meizu: M. Huang Zhang, fondateur de Meizu, agira comme conseiller stratégique pour les produits scientifiques et technologiques de Meizu
- Current situation, trend and view of neural network Internet of things in the future
- Assembly language
- Judge whether the variable is an array
- 瑞能实业IPO被终止:年营收4.47亿 曾拟募资3.76亿
- Kunlun Taike rushes to the scientific innovation board: the annual revenue is 130million, and it plans to raise 500million. CETC Taiji holds 40% of the shares
- Introduction, installation, introduction and detailed introduction to postman!
- Detailed explanation of SSH password free login
- 让秒杀狂欢更从容:大促背后的数据库(下篇)
猜你喜欢

Tiflash compiler oriented automatic vectorization acceleration

牛客网:拦截导弹

In addition to the root directory, other routes of laravel + xampp are 404 solutions

明峰医疗冲刺科创板:年营收3.5亿元 拟募资6.24亿

区间 - 左闭右开

Kunlun Taike rushes to the scientific innovation board: the annual revenue is 130million, and it plans to raise 500million. CETC Taiji holds 40% of the shares

如何深入理解“有限状态机”的设计思想?

Brief introduction to revolutionary neural networks

Anchor navigation demo

Laravel dompdf exports PDF, and the problem of Chinese garbled code is solved
随机推荐
R语言使用nnet包的multinom函数构建无序多分类logistic回归模型、使用coef函数获取模型中每个变量(自变量改变一个单位)对应的对数优势比(log odds ratio)
R language uses boxplot function in native package (basic import package, graphics) to visualize box plot
Tidb DM alarm DM_ sync_ process_ exists_ with_ Error troubleshooting
Matlab learning 2022.7.4
OSI and tcp/ip protocol cluster
Sorter evolution of ticdc 6.0 principle
Judge whether the variable is an array
Liste des liens (simple)
POI set the data format of the column (valid)
VC development of non MFC program memory leak tracking code
R语言使用原生包(基础导入包、graphics)中的boxplot函数可视化箱图(box plot)
R language ggplot2 visualization: gganimate package is based on Transition_ The time function creates dynamic scatter animation (GIF) and uses shadow_ Mark function adds static scatter diagram as anim
Laravel - view (new and output views)
明峰医疗冲刺科创板:年营收3.5亿元 拟募资6.24亿
R语言使用MASS包的polr函数构建有序多分类logistic回归模型、使用coef函数获取模型中每个变量(自变量改变一个单位)对应的对数优势比(log odds ratio)
常见问题之PHP——Fatal error: Allowed memory size of 314572800 bytes exhausted...
深拷贝真难
Use the word "new" to attract curious people
Anchor navigation demo
Which Internet companies are worth going to in Shenzhen for software testers [Special Edition for software testers]