Commonly used regular expression check

1. The expression of the check digit

1 digit: ^[0-9]*$ 

2 n-digit numbers: ^\d{n}$

3 Numbers with at least n digits: ^\d{n,}$ 

4 mn digits: ^\d{m,n}$ 

5 Numbers starting with zero and non-zero: ^(0|[1-9][0-9]*)$ 

6 Non-zero leading numbers with up to two decimal places: ^([1-9][0-9]*)+(.[0-9]{1,2})?$ 

7 Positive or negative numbers with 1-2 decimal places: ^(\-)?\d+(\.\d{1,2})?$ 

8 Positive, negative, and decimal numbers: ^(\-|\+)?\d+(\.\d+)?$ 

9 Positive real numbers with two decimal places: ^[0-9]+(.[0-9]{2})?$

10 Positive real numbers with 1~3 decimal places: ^[0-9]+(.[0-9]{1,3})?$

11 Non-zero positive integer: ^[1-9]\d*$ or ^([1-9][0-9]*){1,3}$ or ^\+?[1-9][0 -9]*$

12 Non-zero negative integer: ^\-[1-9][]0-9"*$ or ^-[1-9]\d*$

13 Non-negative integer: ^\d+$ or ^[1-9]\d*|0$

14 Non-positive integer: ^-[1-9]\d*|0$ or ^((-\d+)|(0+))$

15 Non-negative floating point number: ^\d+(\.\d+)?$ or ^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0? \.0+|0$

16 Non-positive floating-point numbers: ^((-\d+(\.\d+)?)|(0+(\.0+)?))$ or ^(-([1-9]\d*\.\ d*|0\.\d*[1-9]\d*))|0?\.0+|0$

17 Positive float: ^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$ or ^(([0-9]+\.[0- 9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9] *[1-9][0-9]*))$

18 Negative float: ^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)$ or ^(-(([0-9]+ \.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|( [0-9]*[1-9][0-9]*)))$

19 Floating point numbers: ^(-?\d+)(\.\d+)?$ or ^-?([1-9]\d*\.\d*|0\.\d*[1-9]\ d*|0?\.0+|0)$

Second, the expression of the check character

1 Chinese characters: ^[\u4e00-\u9fa5]{0,}$ 

2 English and numbers: ^[A-Za-z0-9]+$ or ^[A-Za-z0-9]{4,40}$ 

3 All characters of length 3-20: ^.{3,20}$ 

4 A string consisting of 26 English letters: ^[A-Za-z]+$ 

5 A string consisting of 26 uppercase English letters: ^[AZ]+$ 

6 A string consisting of 26 lowercase English letters: ^[az]+$ 

7 A string consisting of numbers and 26 English letters: ^[A-Za-z0-9]+$ 

8 A string consisting of numbers, 26 English letters or underscores: ^\w+$ or ^\w{3,20}$ 

9 Chinese, English, numbers including underscore: ^[\u4E00-\u9FA5A-Za-z0-9_]+$

10 Chinese, English, numbers but not including underscores and other symbols: ^[\u4E00-\u9FA5A-Za-z0-9]+$ or ^[\u4E00-\u9FA5A-Za-z0-9]{2,20}$

11 You can enter characters including ^%&',;=?$\": [^%&',;=?$\x22]+

12 Prohibit input of characters containing ~: [^~\x22]+

Three, special needs expression

1 Email地址:^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$ 

2 域名 : [a-zA-Z0-9] [- a-zA-Z0-9] {0,62} (/. [A-zA-Z0-9] [- a-zA-Z0-9] { 0.62}) + /.? 

3 InternetURL:[a-zA-z]+://[^\s]* 或 ^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$ 

4 Mobile phone number: ^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3 |5|6|7|8|9])\d{8}$ 

5 Phone numbers ("XXX-XXXXXXX", "XXXX-XXXXXXXX", "XXX-XXXXXXX", "XXX-XXXXXXXX", "XXXXXXX" and "XXXXXXXX"): ^(\(\d{3,4}-)| \d{3.4}-)?\d{7,8}$  

6 Domestic telephone numbers (0511-4405222, 021-87888822): \d{3}-\d{8}|\d{4}-\d{7} 

7 ID number (15 digits, 18 digits): ^\d{15}|\d{18}$ 

8 Short ID number (number, letter x ending): ^([0-9]){7,18}(x|X)?$ or ^\d{8,18}|[0-9x]{8 ,18}|[0-9X]{8,18}?$ 

9 Whether the account number is legal (begins with letters, allows 5-16 bytes, and allows alphanumeric underscores): ^[a-zA-Z][a-zA-Z0-9_]{4,15}$

10 Password (start with a letter, length between 6~18, can only contain letters, numbers and underscores): ^[a-zA-Z]\w{5,17}$

11 Strong password (must contain a combination of uppercase and lowercase letters and numbers, no special characters, length between 8-10): ^(?=.*\d)(?=.*[az])(?=. *[AZ]).{8,10}$

12 Date format: ^\d{4}-\d{1,2}-\d{1,2}

13 12 months of a year (01~09 and 1~12): ^(0?[1-9]|1[0-2])$

14 31 days of a month (01~09 and 1~31): ^((0?[1-9])|((1|2)[0-9])|30|31)$

15 Input format of money:

16 1. There are four representations of money we can accept: "10000.00" and "10,000.00", and "10000" and "10,000" without "cents": ^[1-9][0-9]*$

17 2. This means any number that does not start with 0, however, it also means that a character "0" is not passed, so we use the following form: ^(0|[1-9][0-9]* )$

18 3. A 0 or a number that does not start with 0. We can also allow a negative sign at the beginning: ^(0|-?[1-9][0-9]*)$

19 4. This means a 0 or a number that may be negative and not 0 at the beginning. Let the user start with 0. Also remove the negative sign, because money can never be negative. What we want to add next is Explain possible fractional parts: ^[0-9]+(.[0-9]+)?$

20 5. It must be stated that there should be at least one digit after the decimal point, so "10." is not passed, but "10" and "10.2" are passed: ^[0-9]+(.[0 -9]{2})?$

21 6. In this way, we stipulate that there must be two digits after the decimal point. If you think it is too harsh, you can do this: ^[0-9]+(.[0-9]{1,2})?$

22 7. This allows users to write only one decimal place. Now we should consider commas in numbers, we can do this: ^[0-9]{1,3}(,[0-9]{3})* (.[0-9]{1,2})?$

23 8.1 to 3 digits, followed by any comma + 3 digits, the comma becomes optional, not required: ^([0-9]+|[0-9]{1,3}(,[0- 9]{3})*)(.[0-9]{1,2})?$

24 Remarks: This is the final result, don't forget that "+" can be replaced with "*" if you think an empty string is acceptable (weird, why?) Finally, don't forget to remove the reverse when using a function. slashes, general errors are here

25 xml 文件 : ^ ([a-zA-Z] + -?) + [A-zA-Z0-9] + \\. [X | X] [m | M] [l | L] $

Regular expression for 26 Chinese characters: [\u4e00-\u9fa5]

27 Double-byte characters: [^\x00-\xff] (including Chinese characters, can be used to calculate the length of the string (the length of a double-byte character is counted as 2, and the length of an ASCII character is counted as 1))

28 Regular expression for blank lines: \n\s*\r (can be used to delete blank lines)

29 The regular expression of HTML tags: <(\S*?)[^>]*>.*?</\1>|<.*? /> (The version circulating on the Internet is too bad, and the above is only partially , still powerless for complex nested tags)

30 Regular expressions for leading and trailing whitespace characters: ^\s*|\s*$ or (^\s*)|(\s*$) (can be used to delete whitespace characters at the beginning and end of a line (including spaces, tabs, etc.) characters, form feeds, etc.), very useful expressions)

31 Tencent QQ number: [1-9][0-9]{4,} (Tencent QQ number starts from 10000)

32 China Postal Code: [1-9]\d{5}(?!\d) (China Postal Code is 6 digits) 33 IP Address: \d+\.\d+\.\d+\.\d+ (Extract Useful for IP address) 34 IP address: ((?:(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d)\\.){ 3}(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d)) 


20 Regular Expressions You Must Know (Can Save You 1,000 Lines of Code)

Regular expressions describe a pattern of string matching, which can be used to check whether a string contains a certain substring, replace a matching substring, or extract a substring that meets a certain condition from a string Wait.

When listing directories, *.txt in dir *.txt or ls *.txt is not a regular expression, because the meaning of * and regular expression * is different.

Regular expressions are constructed in the same way as mathematical expressions are created. That is, small expressions can be combined to create larger expressions using various metacharacters and operators. The components of a regular expression can be single characters, sets of characters, ranges of characters, selections between characters, or any combination of all of these components.

Regular expressions are literal patterns consisting of ordinary characters (such as the characters a through z) and special characters (called "metacharacters"). A pattern describes one or more strings to match when searching for text. A regular expression acts as a template to match a pattern of characters against a searched string.

Regular expressions, a very old and powerful text processing tool, can quickly implement a very complex business logic with only a very short expression statement. If you master regular expressions proficiently, your development efficiency can be greatly improved.

Regular expressions are often used to validate fields or arbitrary strings, such as the following JavaScript code that validates basic date formats:

?
1
2
3
var  reg = /^(\\d{1,4})(-|\\/)(\\d{1,2})\\2(\\d{1,2})$/;
var  r = fieldValue.match(reg);   
if (r== null )alert( 'Date format error!' );

The following are 20 regular expressions that are often used in front-end development:

1. Verify password strength

The strength of the password must be a combination of uppercase and lowercase letters and numbers, no special characters, and a length between 8-10.

?
1
^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$

2. Check Chinese

Strings can only be in Chinese.

?
1
^[\\u4e00-\\u9fa5]{0,}$

 3. A string consisting of numbers, 26 English letters or underscores

?
1
^\\w+$

4. Verify E-mail address

As with passwords, the following are regular check statements for e-mail address compliance.

?
1
[\\w! #$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?

5. Verify ID number

The following is the regular check of the ID number. 15 or 18 bits.

15th place:

?
1
^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$

 18th place:

?
1
^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X)$

6. Calibration date

Date check in "yyyy-mm-dd" format, taking into account leap years.

?
1
^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$

7. Check the amount

Amount check, accurate to 2 decimal places.

?
1
^[0-9]+(.[0-9]{2})?$

8. Verify mobile phone number

The following is the regular expression of mobile phone numbers starting with 13, 15, and 18 in China.

?
1
^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\\d{8}$

9. Determine the version of IE

IE has not been completely replaced at present, and many pages still need to be version compatible. The following is the expression for IE version check.

?
1
^.*MSIE [5-8](?:\\.[0-9]+)?(?!.*Trident\\/[5-9]\\.0).*$

10. Verify IP-v4 address

IP4 regular statement.

\\b(?:(?: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]?)\\b

11. Verify IP-v6 address

IP6 regular statement.

?
1
(([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]))

12. Check the prefix of the URL

应用开发中很多时候需要区分请求是HTTPS还是HTTP,通过下面的表达式可以取出一个url的前缀然后再逻辑判断。

?
1
2
3
4
if  (!s.match(/^[a-zA-Z]+:\\/\\ //))
{
  s =  'http://'  + s;
}

13. 提取URL链接

下面的这个表达式可以筛选出一段文本中的URL。

?
1
^(f|ht){1}(tp|tps):\\/\\/([\\w-]+\\.)+[\\w-]+(\\/[\\w- ./?%&=]*)?

14. 文件路径及扩展名校验

验证文件路径和扩展名

?
1
^([a-zA-Z]\\:|\\\\)\\\\([^\\\\]+\\\\)*[^\\/:*?"<>|]+\\.txt(l)?$

15. 提取Color Hex  Codes

有时需要抽取网页中的颜色代码,可以使用下面的表达式。

?
1
\\ #([a-fA-F]|[0-9]){3,6}

16. 提取网页图片

假若你想提取网页中所有图片信息,可以利用下面的表达式。

?
1
\\< *[img][^\\>]*[src] *= *[\\ "\\']{0,1}([^\\" \\'\\ >]*)

17. 提取页面超链接

提取html中的超链接。

?
1
(<;a\\s*(?!.*\\brel=)[^>;]*)(href= "https?://)((?!(?:(?:www\\.)?'.implode('|(?:www\\.)?', $follow_list).'))[^" ]+)"((?!.*\\brel=)[^>;]*)(?:[^>;]*)>

18. 精炼CSS

通过下面的表达式,可以搜索相同属性值的CSS,从而达到精炼代码的目的。

?
1
^\\s*[a-zA-Z\\-]+\\s*[:]{1}\\s[a-zA-Z0-9\\s. #]+[;]{1}

19. 抽取注释

如果你需要移除HMTL中的注释,可以使用如下的表达式。

<!--(.*?)--> 

20. 匹配HTML标签

通过下面的表达式可以匹配出HTML中的标签。

?
1
</?\\w+((\\s+\\w+(\\s*=\\s*(?: ".*?" | '.*?' |[\\^'">\\s]+))?)+\\s*|\\s*)/?>

真的很有用,掌握这20个正则表达式能让你少写1,000行代码,感兴趣的朋友参考下吧!

正则表达式的相关语法

下面是我找到的一张非常不错的正则表达式 Cheat Sheet,可以用来快速查找相关语法。



学习正则表达式
我在网上看到了一篇相当不错的正则表达式快速学习指南,有兴趣继续深入学习的同学可以参考。

对于新手来说脚本之家小编推荐大家大家看这篇文章:http://www.jb51.net/tools/zhengze.html



正则表达式在线测试工具
regex101是一个非常不错的正则表达式在线测试工具,你可以直接在线测试你的正则表达式哦。



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324822639&siteId=291194637