当前位置:网站首页>Sharing the 12 most commonly used regular expressions can solve most of your problems
Sharing the 12 most commonly used regular expressions can solve most of your problems
2022-07-05 14:14:00 【Front end talent】
english | https://medium.com/frontend-canteen/you-dont-need-to-fully-understand-regex-you-just-need-to-know-these-10-most-used-expressions-197dd2397308
translate | Yang Xiaoai
To be frank , I have never understood regular expressions , I spent a lot of time learning it , But I still don't understand .
Later I learned a truth : I really don't need to fully understand all the principles of regular expressions , Understand some common regular expressions thoroughly , Because the number of regular expressions we really use is limited , I just need to add them to my notebook ( You don't even need to remember ).
today , I collected some web Regular expressions often used in projects , I think it can be solved 50% The above regular expression problem .
Digital
01、 Match only numeric strings
The goal is :
12312
1232121
4353
Regular expressions :
/^\d+$/
Simple explanation :
^ Represents the beginning of a string
\d+ Match one or more digits
$ Represents the end of the string
visualization :
usage :
234,124 Contains illegal characters , , So it goes back to false.
id12313 Contains two illegal characters id , So it goes back to false.
02、 Decimal number
The goal is :
123.123
11
3.14
0.43
0
66
123
Regular expressions :
/^\d+(\.?\d+)?$/
\. Match a single char .
? Said the optional .
() Represents a group
(\.?\d)? Is an optional group
visualization :
usage :
. It should be in the middle of the number , therefore .1 and 12. return false.
03、 Alphanumeric character
We often use this regular expression when detecting user names and passwords .
The goal is :
123
abc
123abc
acRa32EEE
bytefish
Jack2022
Regular expressions :
/^[a-zA-Z0-9]+$/
[a-zA-Z0–9] Match all letters and numbers
visualization :
usage :
04、allow space
If you want to leave spaces in the string , You can write regular expressions like this :
/^[a-zA-Z0-9\s]+$/
\s Match blanks .
usage :
05、 E-mail
Email addresses can have many formats , It's hard to write regular expressions that match email addresses perfectly .
If we add some constraints , Then we have the following writing .
Regular expressions 1
^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$
This is a RegularExpressionValidator stay ASP.NET One used in .
usage :
Regular expressions 2
^\[email protected][a-zA-Z_]+?\.[a-zA-Z]{2,3}$
Simple email expression . Numbers are not allowed in domain names , And it is not allowed to use less than 2 One or more 3 A top-level field of letters ( Before they allow more ).
usage :
06、 password
Like email , Passwords may have different rules . Here are some common rules and related regular expressions .
The rules 1
^[a-zA-Z]\w{8,20}$
In this regular expression , The first character of the password must be a letter , It must contain at least 8 Characters and no more than 20 Characters , And you can't use division letters 、 Characters other than numbers and underscores .
usage :
The rules 2:
/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[^\w\s]).{8,}$/
At least 8 Characters
At least 1 Number characters
At least 1 Lowercase letters
At least 1 Capital letters
At least 1 Special characters
07、 user name
May contain _ and — Alphanumeric string , The length is 3 To 16 Characters .
Example :
bytefish
jon-snow
Rob_Stark
Regular expressions :
/^[a-zA-Z0-9_-]{3,16}$/
usage :
08、 website
Check whether the string is URL
/https?:\/\/(www\.)?[[email protected]:%._\+~#=]{2,256}\.[a-z]{2,6}\b([[email protected]:%_\+.~#()?&//=]*)/
usage :
09、IP Address
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、 date
Date format using delimiters YYYY-MM-dd -
/([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))/
usage :
Date format dd-MM-YYYY Use separator - or . /
/^(?:(?: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 label
take HTML Tags match attributes :
/<\/?[\w\s]*>|<.+[\W]>/
usage :
12、 Phone number
U.S. phone number format
The goal is :
123-456-7890
(123) 456-7890
123 456 7890
123.456.7890
+91 (123) 456-7890
Regular expressions :
^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$
usage :
summary
The above is what I share with you today 12 A common regular expression , I hope these regular expressions are useful to you .
Last , Thanks for reading .
Learn more skills
Please click below official account.
边栏推荐
- R language ggplot2 visual bar graph: visualize the bar graph through the two-color gradient color theme, and add label text for each bar (geom_text function)
- upload (1-6)
- R语言ggplot2可视化条形图:通过双色渐变配色颜色主题可视化条形图、为每个条形添加标签文本(geom_text函数)
- R language ggplot2 visualization: visual line graph, using legend in theme function The position parameter defines the position of the legend
- Discussion on memset assignment
- R language ggplot2 visualization: use ggplot2 to visualize the scatter diagram, and use the labs parameter to customize the X axis label text (customize X axis labels)
- 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
- 别不服气。手机功能升级就是强
- Laravel - model (new model and use model)
- 最简单不用证书也可以多开功能的方式
猜你喜欢
国富氢能冲刺科创板:拟募资20亿 应收账款3.6亿超营收
神经网络物联网未来发展趋势怎么样
What are the advantages and characteristics of SAS interface
物联网应用技术专业是属于什么类
ASP.NET大型外卖订餐系统源码 (PC版+手机版+商户版)
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
Redis如何实现多可用区?
TiFlash 源码解读(四) | TiFlash DDL 模块设计及实现分析
Getting started with rce
Make the seckill Carnival more leisurely: the database behind the promotion (Part 2)
随机推荐
Google EventBus 使用详解
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
Current situation, trend and view of neural network Internet of things in the future
最长公共子序列 - 动态规划
Detailed explanation of IP address and preparation of DOS basic commands and batch processing
Mingfeng medical sprint technology innovation board: annual revenue of 350million yuan, proposed to raise 624million yuan
The forked VM terminated without saying properly goodbye
R语言ggplot2可视化:可视化折线图、使用theme函数中的legend.position参数自定义图例的位置
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
What is the ranking of GF futures? Is it safe and reliable to open an account for GF futures online?
Enjoy what you want. Zhichuang future
Mysql database installation tutorial under Linux
Judge whether the variable is an array
关于memset赋值的探讨
What is the future development trend of neural network Internet of things
LeetCode_3(无重复字符的最长子串)
MySQL user-defined function ID number to age (supports 15 / 18 digit ID card)
动态规划
国富氢能冲刺科创板:拟募资20亿 应收账款3.6亿超营收
微服务项目部署后,无法访问静态资源,无法访问到上传到upload中的文件,解决办法