Commonly used regular expressions

regular expression

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 JavaScriptcode that validates basic date formats:

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

Check 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 .

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

Check Chinese

Strings can only be in Chinese.

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

A string consisting of numbers, 26 English letters or underscores

^\\w+$

Verify E-Mail Address

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

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

Verify ID number

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

15th place

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

18th place

^[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)$

check date

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

^(?:(?!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)$

check amount

Amount check, accurate to 2 decimal places .

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

Verify phone number

The following is the regular expression of mobile phone numbers starting with 13, 15, and 18 in China. (The first two numbers can be expanded according to the current domestic collection 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}$

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.

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

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

Verify IP-v6 address

IP6 regular statement.

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

Check the prefix of the URL

In application development, it is often necessary to distinguish whether the request is HTTPS or HTTP. Through the following expression, a url prefix can be extracted and then logically judged.

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

Extract URL links

The following expression filters out URLs in a piece of text.

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

File path and extension verification

Verify the file path and extension under Windows (.txt file in the example below)

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

Extract Color Hex Codes

Sometimes it is necessary to extract the color code in the web page, you can use the following expression.

^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$

Extract web images

If you want to extract all image information in a web page, you can use the following expression.

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

Extract page hyperlinks

Extract hyperlinks in html.

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

Find CSS properties

Through the following expressions, you can search for matching CSS properties.

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

Extract annotations

If you need to remove comments in HMTL, you can use the following expressions.

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

match HTML tags

The tag attributes in HTML can be matched by the following expressions.

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

Syntax related to regular expressions

Regular expression related syntax.jpg

learn regular expressions

I saw a pretty good regular expression quick learning guide on the Internet , and students who are interested in continuing to learn more can refer to it.

Regular expression online testing tool

Online Regular Expression Test

Online Regular Expression Test.png


regex101

regex101.png


Original link


Guess you like

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