A collection of common regular expressions

create by db on 2019-5-13 09:45:24

Recently revised in 2019-6-21 11:30:54

Hello friends, if you think this article is good, please give a thumbs up or a star, your thumbs up and star are my motivation! Making the address

preface

I hear and I fogorget.

I see and I remember.

I do and I understand.

As a front-end coder, you can’t avoid using regex to validate data or intercept strings. However, for these “Martian words” are always unreliable. So keep a file of regular expressions you use at work to keep a base for yourself and your friends.

This document does not do regular popular science details, only for reference. If you don’t know exactly what a re is, please refer to the references.

References:

  • Introduction to regular expressions in js | blog garden – Chen Shuishui personal blog

The body of the

Common operators of regular expressions

The operator instructions The instance
. Represents any single character
[] Character set, giving a range for a single character [abc]Indicates a, B, or C. [a-z] indicates a single character from A to Z
(^) Non – character set, given the exclusion range for a single character [^abc]A single character that is not a or B or C
_ The preceding character expands zero or infinitely abc_The value can be AB, ABC, ABCC, or ABCCC
| Any of the left and right expressions abc|defABC, def
$ Matching the end of a string abc$Represents ABC and ends in a string
( ) Group markers can only be used internally (abc)According to ABC,(abc|def)ABC, def
\D The digital
\d Numbers, equivalent to 0 minus 9
\S Visible characters
\s White space characters
\W Non-word character
\w Word character, equivalent to [A-z0-9a-z_]
^ Matching the beginning of a string ^abcRepresents ABC at the beginning of a string
{m,n} Extend the previous character m to n times Ab c {1, 2}Indicates ABC and ABBC
{m} Expand the previous character m times ab{2}cSaid abbc
? The preceding character is expanded 0 or 1 times abc?Indicates AB and ABC

undefined

A collection of commonly used regular expressions

One, the expression of the check number

The Numbers: ^ [0-9] * $

N digits: ^d{n}$

At least n digits: ^d{n,}$

M -n digits: ^d{m,n}$

The number of zero and non-zero start: ^ (0 | [1-9] [0-9] *) $

Non-zero digits with at most two decimal digits: ^([1-9][0-9]*)+(.[0-9]{1,2})? $

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

Positive, negative, and decimal: ^ (+) – |? d+(.d+)? $

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

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

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

Non-zero negative integers: ^-[1-9][]0-9″*$or ^-[1-9]d*$

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

A positive integer: ^ – [1-9] d * | 0 $or ^ (+) (- d | (0 +)) $

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

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

Are floating point Numbers: ^ 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] *)) $

Negative floating point Numbers: ^ – (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] *))) $

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

Second, the expression of the verification character

Chinese characters: ^ + $[\ u4e00 – \ u9fa5]

^[a-za-z0-9]+$or ^[a-za-z0-9]{4,40}$

All characters from 3 to 20: ^.{3,20}$

The value is A string of 26 letters: ^[a-za-z]+$

The value contains 26 uppercase letters: ^[a-z]+$

The value is a string of 26 lowercase letters: ^[a-z]+$

The value is A string of 26 letters and digits: ^[A-za-z0-9]+$

The value is a string of 26 letters, digits, and underscores (_) : ^w+$or ^w{3,20}$

Special requirements expression

Removes white Spaces at the left and right sides: STR. Replace (/ (^ \ s *) | (\ s * $)/g, “)

Remove all Spaces: str.replace(/\s+/g, “)

The password must contain at least eight uppercase letters, lowercase letters, digits, and special characters: /^(? = (. *? [A-Z])(? = (. *? [a-z])(? = (. *? ([0-9])? = (. *? [#?!, @ $% ^ & * -]) {8} $/

Email address: ^ W +([-+.] W +)* @W +([-.] W +)*.w+([-.] W +)*$

Domain name: [a – zA – Z0-9] [9] – a – zA – Z0 – on conversion {0} (/. [a zA – Z0-9] [9] – a – zA – Z0 – on conversion {0}) + /.?

InternetURL: [a-za-z]+://[^s]* or ^http://([w-]+.) +[w-]+(/[w-./?%&=]*)? $`

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

Phone number (- XXXXXXX “XXX”, “XXXX – XXXXXXXX”, “XXX – – XXXXXXXX” XXXXXXX “and” XXX, “XXXXXXX” and “XXXXXXXX) : ^ ({3, 4} – (d) | d} {3.4 -)? D {7, 8} $

Home phone number (0511-4405222, 021-87888822) : d {8} {3} – d | d {7} {4} – d

Id number (15, 18 digits) : ^ d {15} {and} $| d

Short id number (Numbers, the letter “x” at the end) : ^ ([0-9] {7, 17} (x | x)? ${8} 16 or ^ d | x [0-9] {8} 16 | x [0-9] {8} 16? $

Account is (legal letter, allow 5-16 bytes, allow alphanumeric underlined) : ^ [a zA – Z] [a zA – Z0-9 _] {4, 15} $

Password (must start with a letter and contain only letters, digits, and underscores (_).) : ^[a-za-z]w{5,17}$

Strong password (must contain uppercase and lowercase letters and digits, cannot use special characters, and is between 8 and 10 characters in length) : ^(? =.*d)(? =.*[a-z])(? =. * [a-z]). 8, 10 {} $

^d{4}-d{1,2}-d{1,2}

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

31 days of a month (01 ~ 09 and 1 ~ 31) : ^((0? [1-9]) | | 2 (1) ([0-9]) | | 30 31) $XML file: ^ ([a zA – Z] + -?) +[a-zA-Z0-9]+\.[x|X][m|M][l|L]$

Regular expression for blank lines: s* (can be used to delete blank lines)

Regular expressions for HTML tags: <(S*?) [` ^ >] * >. *? < / a > | <. *? /> < span style = “max-width: 100%; clear: both; min-height: 1em;

Fore and aft blank characters of regular expressions: ^ s * * | s $or (^ s *) | * $(s) (first line can be used to delete rows of white space characters (including Spaces, tabs, form-feed character, etc.), very useful expressions)

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

China Postcode: [1-9]d{5}(? ! D) (6-digit Postal code in China)

IP address: d+.d+.d+.d+

IP address :((? : (? :25[0-5]|2[0-4]\d|[01]? \d? \d)\.) {3} (? :25[0-5]|2[0-4]\d|[01]? \d? \d))

Input format of money:

  1. There are four forms of money we can accept: “10000.00” and “10,000.00”, and “10000” and “10,000” without “cent” : ^[1-9][0-9]*$

  2. This means that any one does not begin with 0, but it also means that a character “0” is not through, so we use the form below: ^ (0 | [1-9] [0-9] *) $

  3. A 0 or a number that doesn’t start with 0. We can also allow the beginning there is a minus sign: ^ (0 | -? [1-9] [0-9] *) $

  4. This represents a 0 or a number that might be negative and doesn’t start with a 0. Let the user start with 0. Let’s get rid of the minus, because money can’t be negative. ^[0-9]+(.[0-9]+)? $

  5. Must be clear, decimal point should be at least 1 digit, so “10.” it is not through, but the “10” and “10.2” is through: ^ [0-9] + (. [0-9] {2})? $

  6. So we provisions of decimal point must be two, if you think that is too harsh, it can be: ^ [0-9] + (. [0-9] {1, 2})? $

  7. This allows the user to write only one decimal place. Below we should consider a comma in the digital, we can be like this: ^ [0-9] {1, 3} ([0-9] {3}), * (. [0-9] {1, 2})? $

  8. 1 to 3 Numbers, followed by any comma + 3 Numbers, commas become optional, rather than having to: ^ ([0-9] + | [0-9] {1, 3} ([0-9] {3}), * (. [0-9] {1, 2})? $

A simple way to use regular expressions

Using the test() method of JavaScript to determine whether the character is Chinese, write a function.

  • test()The method is used to check whether a string matches a pattern. Returns true if there is a matching value in the string, false otherwise.
// Whether to include Chinese (also including Japanese and Korean)
function isChineseChar(str){   
   var reg = /[\u4E00-\u9FA5\uF900-\uFA2D]/;
   return reg.test(str);
}

isChineseChar('122') //false
isChineseChar('One, two, three') //true
Copy the code

conclusion

As a front-end novice, the purpose of this article is to record their learning experience, if there are shortcomings, please also give advice. Most of them are copied, if there are mistakes, slowly use, slowly change. Thank you.

The road ahead is long, and I see no end.

Postscript: Hello friends, if you think this article is good, remember to give a thumbs-up or star, your thumbs-up and star is my motivation to write more and richer articles!Making the address



dbThe document library 由 dbusingCreative Commons Attribution – Non-commercial Use – Same way Share 4.0 International LicenseGrant permission.

Based on thegithub.com/danygitgitOn the creation of works.

Use rights other than those authorized by this License agreement may be obtained fromCreativecommons.org/licenses/by…Obtained.