当前位置:网站首页>分享 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个常用的正则表达式,希望这些正则表达式对你有用。
最后,感谢你的阅读。
学习更多技能
请点击下方公众号
![]()
边栏推荐
- 03_Solr之dataimport
- POI set the data format of the column (valid)
- 基于 TiDB 场景式技术架构过程 - 理论篇
- Simple process of penetration test
- Some ideas about Apache mesos
- How to deeply understand the design idea of "finite state machine"?
- LeetCode_67(二进制求和)
- 汇编语言 assembly language
- The IPO of Ruineng industry was terminated: the annual revenue was 447million and it was planned to raise 376million
- 广发期货排名多少?网上办理广发期货开户安全可靠吗?
猜你喜欢

What are the advantages and characteristics of SAS interface

Sorter evolution of ticdc 6.0 principle

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

What is the future development trend of neural network Internet of things

OSI and tcp/ip protocol cluster

openGauss数据库源码解析系列文章—— 密态等值查询技术详解(下)

Introduction, installation, introduction and detailed introduction to postman!

Guofu hydrogen energy rushes to the scientific and Technological Innovation Board: it plans to raise 2billion yuan, and 360million yuan of accounts receivable exceed the revenue

治臻新能源冲刺科创板:年营收2.2亿 上汽创投是股东

软件测试人在深圳有哪些值得去的互联网公司【软件测试人员专供版】
随机推荐
Comparison of several distributed databases
The simplest way to open more functions without certificates
区间 - 左闭右开
Which Internet companies are worth going to in Shenzhen for software testers [Special Edition for software testers]
LeetCode_3(无重复字符的最长子串)
What is the future development trend of neural network Internet of things
一网打尽异步神器CompletableFuture
基于伯努利原理的速度监测芯片可用于天然气管道泄露检测
魅族新任董事长沈子瑜:创始人黄章先生将作为魅族科技产品战略顾问
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
魅族新任董事長沈子瑜:創始人黃章先生將作為魅族科技產品戰略顧問
Guofu hydrogen energy rushes to the scientific and Technological Innovation Board: it plans to raise 2billion yuan, and 360million yuan of accounts receivable exceed the revenue
tidb-dm报警DM_sync_process_exists_with_error排查
What are the advantages and characteristics of SAS interface
关于memset赋值的探讨
瑞能实业IPO被终止:年营收4.47亿 曾拟募资3.76亿
根据CronSequenceGenerator计算cron表达式的时间
Blue Bridge Cup study 2022.7.5 (morning)
Recommendation number | what are interesting people looking at?
展现强大。这样手机就不会难前进