当前位置:网站首页>分享 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个常用的正则表达式,希望这些正则表达式对你有用。
最后,感谢你的阅读。
学习更多技能
请点击下方公众号
![]()
边栏推荐
- 汇编语言 assembly language
- 常见问题之PHP——Fatal error: Allowed memory size of 314572800 bytes exhausted...
- JS takes key and value from an array object to form a new object
- Blue Bridge Cup study 2022.7.5 (morning)
- 01 、Solr7.3.1 在Win10平台下使用jetty的部署及配置
- The IPO of Ruineng industry was terminated: the annual revenue was 447million and it was planned to raise 376million
- After the microservice project is deployed, static resources and files uploaded to upload cannot be accessed. Solution
- Matlab learning 2022.7.4
- How to deeply understand the design idea of "finite state machine"?
- C语言中限定符的作用
猜你喜欢

How to deeply understand the design idea of "finite state machine"?

Anchor navigation demo

Simple process of penetration test

Introduction, installation, introduction and detailed introduction to postman!

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

Financial one account Hong Kong listed: market value of 6.3 billion HK $Ye wangchun said to be Keeping true and true, long - term work

TiFlash 源码解读(四) | TiFlash DDL 模块设计及实现分析

无密码身份验证如何保障用户隐私安全?

Lepton 无损压缩原理及性能分析

Brief introduction to revolutionary neural networks
随机推荐
金融壹賬通香港上市:市值63億港元 葉望春稱守正篤實,久久為功
[buuctf.reverse] 152-154
国富氢能冲刺科创板:拟募资20亿 应收账款3.6亿超营收
Interpretation of tiflash source code (IV) | design and implementation analysis of tiflash DDL module
Recommendation number | what are interesting people looking at?
Tiflash compiler oriented automatic vectorization acceleration
关于Apache Mesos的一些想法
PHP5下WSDL,SOAP调用实现过程
R语言使用MASS包的polr函数构建有序多分类logistic回归模型、使用coef函数获取模型中每个变量(自变量改变一个单位)对应的对数优势比(log odds ratio)
Requests + BS4 crawl Douban top250 movie information
登录界面代码
R language ggplot2 visual density map: Visual density map by group and custom configuration geom_ The alpha parameter in the density function sets the image transparency (to prevent multiple density c
2022 construction welder (special type of construction work) special operation certificate examination question bank and online simulation examination
SSH免密码登录详解
Simple process of penetration test
治臻新能源冲刺科创板:年营收2.2亿 上汽创投是股东
区间 - 左闭右开
Leetcode array question brushing notes
2022 driller (drilling) examination question bank and simulation examination
TiCDC 6.0原理之Sorter演进