Common Regular Expressions Collection

@ Common Regular Expressions Collection

create by db on 2019-5-13 09:45:24
Recently revised in 2019-5-14 14:47:50

Hello little friends, if you feel that this article is not bad, praise or to a trouble spot a star, and the star of your praise is my motivation! GitHub address

Foreword

I hear and I fogorget.

I see and I remember.

I do and I understand.

 As a front end code agriculture, can not always be avoided using regular or taken to verify the data string. However insecure for these "Martian" always in mind. Therefore, the work of some common regular expressions finishing the archive, give yourself a bottom, but also a friend to be a reference.

 This document is not a regular science explain, only for reference. If it is not too clear what is regular, please reference the venue.

 references:

text

Common regular expression operators

Operators Explanation Examples
. It represents any single character
[ ] Character Set, ranges are given for a single character [abc]Represents a, b, c, [az] az represents a single character
[^ ] Non characters, a single character is given to the negative range [^abc]It represents a or b or c non-single character
_ Previous character zero or infinite extension abc_Represents ab, abc, abcc, abccc etc.

Common Regular Expressions Collection

First, the check digit of expression

digital:^[0-9]*$

n-bit numbers:^d{n}$

At least n-bit numbers:^d{n,}$

mn digits:^d{m,n}$

Zero and non-zero number that begins:^(0|[1-9][0-9]*)$

With up to two decimal places beginning with a non-zero:^([1-9][0-9]*)+(.[0-9]{1,2})?$

Decimals with 1-2 positive or negative:^(-)?d+(.d{1,2})?$

Positive, negative, and decimals:^(-|+)?d+(.d+)?$

There are two positive real number of decimal places:^[0-9]+(.[0-9]{2})?$

1 to 3 a positive real number of decimal places:^[0-9]+(.[0-9]{1,3})?$

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

Negative integer zero: ^-[1-9][]0-9"*$or^-[1-9]d*$

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

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

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

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

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]*))$

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]*)))$

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

Second, check character expression

Chinese character:^[\u4e00-\u9fa5]+$

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

Length of all characters 3-20:^.{3,20}$

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

The 26 uppercase letters of the English string:^[A-Z]+$

Write letters composed by the 26 small string:^[a-z]+$

By the numeral 26 and letters of the English string:^[A-Za-z0-9]+$

String of digits, letters or underscore 26 consisting of: ^w+$or^w{3,20}$

Third, the special needs of expression

Remove about blank: str.replace(/(^\s*)|(\s*$)/g, '')

Remove all spaces: str.replace(/\s+/g, '')

Password need more than 8 bits by the uppercase letters, lowercase letters, numbers and special symbols: /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!.,@$%^&*-]).{8,}$/

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

域名: [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-./?%&=] *), $ `

cellphone 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}$

Telephone number ( "XXX-XXXXXXX", "XXXX-XXXXXXXX", "XXX-XXXXXXX", "XXX-XXXXXXXX", "XXXXXXX" and "XXXXXXXX):^((d{3,4}-)|d{3.4}-)?d{7,8}$

Domestic phone numbers(0511-4405222、021-87888822):d{3}-d{8}|d{4}-d{7}

ID number (15, 18 numbers):^d{15}|d{18}$

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

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

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

Strong passwords (which must contain a combination of uppercase and lowercase letters and numbers, special characters can not be used, a length of between 8 and 10):^(?=.*d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$

Date Format:^d{4}-d{1,2}-d{1,2}

Year 12 months (01 ~ 09 and 1 to 12):^(0?[1-9]|1[0-2])$

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

N blank lines of expression: s*(can be used to delete blank lines)

HTML markup regular expression: <(S*?)[`^>]*>.*?</>|<.*? />(circulated on the Internet version too bad, this is only partially above, for complex nested tags still powerless)

And last whitespace regular expression: ^s*|s*$or ( ^s*)|(s*$) (can be used to remove white space character of the line end of the line (including spaces, tabs, page breaks, etc.), very useful expression)

Tencent QQ number: [1-9][0-9]{4,}(Tencent QQ number from 10,000 to start)

China Postal Code: [1-9]d{5}(?!d)(6 digits ZIP code for China)

IP Address: d+.d+.d+.d+(IP address is useful when extracting)

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

Money input formats:

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

  2. This does not represent any of a number from 0 to begin with, but it also means that a character "0" is not passed, so we take the following form:^(0|[1-9][0-9]*)$

  3. A 0 or a number from 0 to not begin with we can also allow the beginning of a negative sign:^(0|-?[1-9][0-9]*)$

  4. This represents a 0 or a possible negative without the leading digital 0. allows users to start with 0 as well. The minus sign also removed, because the money can not be negative right. Now we want to add is to explain possible decimal part:^[0-9]+(.[0-9]+)?$

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

  6. We must have regulations after the decimal point two, if you think too hard, it can be:^[0-9]+(.[0-9]{1,2})?$

  7. This allows users to write only one decimal Here we consider the numbers in the comma, we can:^[0-9]{1,3}(,[0-9]{3})*(.[0-9]{1,2})?$

  8. 1-3 digits, followed by any number of commas +3 digits, comma become optional, but not required:^([0-9]+|[0-9]{1,3}(,[0-9]{3})*)(.[0-9]{1,2})?$

Simple to use regular expressions

 To determine whether the Chinese characters, for example, using JavaScript test()method, write a function.

  • test() A method for detecting whether a character string matching a pattern. If there is a value matching string returns true, otherwise returns false.
//是否含有中文(也包含日文和韩文)
function isChineseChar(str){   
   var reg = /[\u4E00-\u9FA5\uF900-\uFA2D]/;
   return reg.test(str);
}

isChineseChar('122') //false
isChineseChar('一二三') //true
复制代码

to sum up

 As a front-end rookie, this article is intended to record their own learning experience, if insufficient, also requested the exhibitions. Most of which are copies of any error, by slowly, slowly change. thank you all.

 The road is long Come, gentlemen and encourage each other.

Postscript: Hello little friends, if you think this article pretty good, I remember a point or to praise a star, your star is like and I write more articles richer power! GitHub address


db document library by the db using Creative Commons Attribution - NonCommercial - ShareAlike 4.0 International License Agreement for licensing.
Based github.com/danygitgit works on creation.
Usage rights other than this license agreement grants from creativecommons.org/licenses/by... get at.

Reproduced in: https: //juejin.im/post/5d024d665188254c9f4feae3

Guess you like

Origin blog.csdn.net/weixin_33774883/article/details/93175227