Regular expression rules and common cases

Rule symbol

Regular classification symbol meaning Remarks
Match position ^ beginning  
$ end  
Predefined \d 1 number Simplification of commonly used character sets
\b Word boundary
\w 1 number or letter
\s 1 empty character (space, line feed, tab)
. Match all characters except carriage return and line feed
antonym \D Match any non-digit character The letter is capitalized, and the meaning is opposite to the predefined.
\B Match is not at the beginning or end of a word
\W Match any character that is not a letter, number, or underscore
\S Match any character that is not a whitespace
[^X] Except X Exclude, ^ can only be placed at the beginning.
quantifier {n,m} At least n times, at most m times The character set is written before the quantifier.
The quantifier modifies the adjacent previous (left) character set.
{n,} At least n times
{n} Must be n times
? At most 1 time
* Dispensable
+ At least once
Lazy qualifier *? Repeat as many times as possible, but as little as possible Repeat as little as possible
+? Repeat 1 or more times, but as little as possible
?? Repeat 0 or 1 times, but repeat as little as possible
{n,m}? Repeat n to m times, but repeat as little as possible
{n,}? Repeat more than n times, but repeat as little as possible
Shorthand [A-Za-z] 1 letter The middle character is continuous and can be used-omit abbreviations.
[0-9] 1 number
[\u4e00-\u9fa5] 1 Chinese character
Escape character \ Make special characters into ordinary characters to be recognized, such as \+ Tell the browser to escape the characters as the original text
select Rule 1 | Rule 2 or Just satisfy one rule
Grouping () Wrap multiple character sets with () In order to make a quantifier modify multiple character sets
(exp) Match exp and capture the text into the automatically named group Capture group
(?<name>exp) Match exp, and capture the text into the group named name
(?:exp) Match exp, do not capture the matched text, nor assign a group number to this group
(?=exp) Match the position before exp
(?<=exp) Match the position behind exp
(?!exp) Match the position that is not followed by exp
(?<!exp) Match the position that is not exp before

 

Common cases

Common case Rule description Regular expression
identity number The citizenship number is a characteristic combination code, which is composed of a 17-digit body code and a check code. The sequence from left to right is: six-digit address code, eight-digit date of birth code, three-digit sequence code and one-digit check code. \d{15}(\d{2}[0-9xX])?
\d{17}[\d|x|X]|\d{15}
手机号 11位手机号 ^1[0-9]{10}$
11位手机号,约束第二位为 3 4 5 7 8 ^1[34578]\d{9}$
邮箱 邮箱不区分大小写 \w 匹配数字母下划线 中括号里是小写字母或0-9数字 后面+是一个或多个 转义一个点  任意多个字母 /^\w+@[a-z0-9]+\.[a-z]+$/i
  ^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$
密码 至少8字符 至少1大写字母 至少1小写字母 至少1数字字符 至少1特殊字符 /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[^\w\s]).{8,}$/
至少8-16个字符,至少1个大写字母,1个小写字母和1个数字,其他字符任意 /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[^]{8,16}$/
以字母开头,长度在6~18之间,只能包含字母、数字和下划线 ^[a-zA-Z]\w{5,17}$
必须包含大小写字母和数字的组合,不能使用特殊字符,长度在 8-10 之间 ^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z0-9]{8,10}$
必须包含大小写字母和数字的组合,可以使用特殊字符,长度在8-10之间 ^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$
域名   [a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+\.?
InternetURL   [a-zA-z]+://[^\s]* 或 ^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$

Guess you like

Origin blog.csdn.net/Irene1991/article/details/105072548