What are the more commonly used regular expressions
This article mainly introduces the more commonly used regular expressions, which have a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.
Regular expressions (Regular Expression, often abbreviated as regex, regexp, or RE in code) is a concept in computer science. Regular expressions use a single string to describe and match a series of strings that conform to a syntactic rule. In many text editors, regular expressions are often used to retrieve and replace text that matches a certain pattern. Many programming languages support string manipulation using regular expressions. In many text editors, regular expressions are often used to retrieve and replace text that matches a certain pattern.
Regular expression that matches Chinese characters: [u4e00-u9fa5]
Note: matching Chinese is really a headache. It's easy to have this expression.
Match double-byte characters (including Chinese characters): [^ x00-xff]
Note: can be used to calculate the length of a string (a double-byte character length meter 2 ~ ~ ASCII character counter 1)
A regular expression that matches a blank line: ns*r
Comment: can be used to delete blank lines
Regular expression that matches the HTML tag:] * >. *? |
Commentary: the version circulated on the Internet is too bad, the above one can only match part of it, and there is still nothing I can do about complex nested tags.
Regular expression that matches the leading and trailing white space characters: ^ s* | slots $
Comment: a very useful expression that can be used to delete white space characters (including spaces, tabs, page feeds, etc.) at the beginning and end of a line
Regular expression that matches the Email address: W + ([- +.] w +) * @ w + ([-.] w +) * .w + ([-.] w +) *
Comment: form validation is very useful
Regular expression that matches the URL URL: [a-zA-z] +: / / [^ s] *
Note: the function of the version circulated on the Internet is very limited, and the above version can basically meet the needs.
Whether the matching account is legal (5-16 bytes are allowed at the beginning of the letter, and alphanumeric underscores are allowed): ^ [a-zA-Z] [a-zA-Z0-9 _] {4j 15} $
Comment: form validation is very useful
Match domestic phone number: d {3}-d {8} | d {4}-d {7}
Commentary: matching forms such as 0511-4405222 or 021-87888822
Match Tencent QQ number: [1-9] [0-9] {4,}
Commentary: Tencent QQ starts from 10000
Match the postcode of China: [1-9] d {5} (?! d)
Commentary: the postal code of China is 6 digits
Match ID: d {15} | d {18}
Commentary: Chinese ID cards are 15 or 18 digits
Match the ip address: dong.dharm.ding.d+
Note: useful when extracting ip addresses
Match specific numbers:
^ [1-9] $/ / match positive integers
^-[1-9] $/ / match negative integers
^ -? [1-9] d _ match $/ / match integer
^ [1-9] d * | 0 $/ / matches a non-negative integer (positive integer + 0)
^-[1-9] d * | 0 $/ / matches non-positive integers (negative integers + 0)
^ [1-9] dquo.d* | 0.d* [1-9] droom$ / / matches positive floating point numbers
^-([1-9] dfol.d* | 0.d* [1-9] d*) $/ / matches negative floating point numbers
^ -? ([1-9] droom.d* | 0.d* [1-9] d* | 0room.0+ | 0) $/ / match floating point number
^ [1-9] dfol.d* | 0.d* [1-9] d* | 0room.0+ | 0 $/ / match non-negative floating point number (positive floating point number + 0)
^ (- ([1-9] dfu.d* | 0.d* [1-9] d*)) | 0room.0+ | 0 $/ / matches non-positive floating point numbers (negative floating point numbers + 0)
Commentary: useful when dealing with a large amount of data, pay attention to corrections in specific applications
Match a specific string:
^ [A-Za-z] + $/ / matches a string of 26 English letters
^ [Amurz] + $/ / matches a string of 26 English letters in uppercase
^ [amurz] + $/ / matches a string of 26 lowercase letters
^ [A-Za-z0-9] + $/ / matches a string of numbers and 26 letters
^ wrist $/ / matches a string consisting of numbers, 26 letters, or underscores
The validation features and their validation expressions when using RegularExpressionValidator validation controls are described as follows:
You can only enter a number: "^ [0-9] * $"
Only n-digit numbers can be entered: "^ d {n} $"
You can only enter at least n digits: "^ d {n,} $"
You can only enter the number of mmurn digits: "^ d {mdirection n} $"
You can only enter zero and non-zero numbers: "^ (0 | [1-9] [0-9] *) $"
You can only enter positive real numbers with two decimal places: "^ [0-9] + (. [0-9] {2})? $"
You can only enter positive real numbers with 1-3 decimal places: "^ [0-9] + (. [0-9] {1pr 3})? $"
You can only enter a positive integer that is not zero: "^ +? [1-9] [0-9] * $"
You can only enter a negative integer that is not zero: "^-[1-9] [0-9] * $"
Only characters of length 3 can be entered: "^. {3} $"
You can only enter a string of 26 letters: "^ [A-Za-z] + $"
You can only enter a string of 26 capital letters: "^ [Amurz] + $"
You can only enter a string of 26 lowercase letters: "^ [amurz] + $"
You can only enter a string of numbers and 26 letters: "^ [A-Za-z0-9] + $"
You can only enter a string consisting of numbers, 26 letters, or an underscore: "^ wicked $"
Verify the user password: "^ [a-zA-Z] w {5pm 17} $" is in the correct format: begins with a letter and is between 6 and 18 in length
Can only contain characters, numbers, and underscores.
Verify that it contains characters such as ^% &',; =? $":" [^% &',; =? $x22] + "
You can only enter Chinese characters: "^ [u4e00-u9fa5], {0,} $"
Verify the Email address: "^ w + [- +.] w +) * @ w + ([-.] w +) * .w + ([-.] w +) * $"
Verify InternetURL: "^ http://([w-]+.)+[w-]+(/[w-./?%&=]*)?$"
Verify the phone number: "^ ((d {3pr 4}) | d {3pr 4} -)? d {7pr 8} $"
The correct format is "XXXX-XXXXXXX", "XXXX-XXXXXXXX", "XXX-XXXXXXX"
"XXX-XXXXXXXX", "XXXXXXX", "XXXXXXXX".
Verify ID number (15 or 18 digits): "^ d {15} | d {} 18 $"
Verify the 12 months of the year: "^ (0? [1-9] | 1 [0-2]) $" the correct format is: "01"-"09" and "1"12"
Verify 31 days of a month: "^ ((0? [1-9]) | (1 | 2) [0-9]) | 30 | 31) $"
The correct formats are "01", "09" and "1"31".
Regular expression that matches Chinese characters: [u4e00-u9fa5]
Match double-byte characters (including Chinese characters): [^ x00-xff]
Regular expression that matches blank lines: n [s |] * r
Regular expression that matches the HTML tag: /. * | /
Regular expression that matches leading and trailing spaces: (^ s*) | (sworn $)
Regular expression that matches the Email address: W + ([- +.] w +) * @ w + ([-.] w +) * .w + ([-.] w +) *
The regular expression that matches the URL URL: http://([w-]+.)+[w-]+(/[w-. /?% & =] *?
(1) Application: calculate the length of a string (a double-byte character length meter 2 ~ ~ ASCII character counter 1)
String.prototype.len=function () {return this.replace ([^ x00-xff] / g, "aa") .length;}
(2) Application: if there is no trim function like vbscript in javascript, we can use this expression to implement
String.prototype.trim = function () {return this.replace (/ (^ s*) | (swarm $) / g, ");}
(3) Application: using regular expressions to decompose and translate IP addresses
Function IP2V (ip) / / IP address is translated into the corresponding value {re=/ (d +). (d +). (d +) / g / / the regular expression if (re.test (ip)) {return RegExp.$1*Math.pow (255prime2)) + RegExp.$2*Math.pow (255ping2)) + RegExp.$3*255+RegExp.$4*1} else { Throw new Error ("Not a valid IP address!")}}
(4) Application: javascript program that extracts file names from URL addresses
S = "http://www.9499.net/page1.htm";
S=s.replace (/ (. * /) {0,} ([^.] +). * / ig, "$2"); / / Page1.htm
(5) Application: use regular expressions to restrict the input of text boxes in web forms.
Use regular expressions to restrict input to Chinese only: onkeyup= "value=value.replace (/ [^ u4E00-u9FA5] / gheroine')
"onbeforepaste=" clipboardData.setData (''text'',clipboardData.getData (' 'text''). Replace (/ [^ u4E00-u9FA5] / g text''')) "
Use regular expressions to restrict the input of full-width characters: onkeyup=value=value.replace (/ [^ uFF00-uFFFF] / gheroine')
"onbeforepaste=" clipboardData.setData (''text'',clipboardData.getData (' 'text''). Replace (/ [^ uFF00-uFFFF] / g text''')) "
Use regular expressions to restrict input to only numbers: onkeyup= "value=value.replace (/ [^ d] / gheroine')" onbeforepaste=
"clipboardData.setData (''text'',clipboardData.getData (' 'text''). Replace (/ [^ d] / gheroine'))"
Use regular expressions to limit input to numbers and English: onkeyup= "value=value.replace (/ [W] / gjinghee')
"onbeforepaste=" clipboardData.setData (''text'',clipboardData.getData (' 'text''). Replace (/ [^ d] / gmeme')
Thank you for reading this article carefully. I hope the article "what are the more commonly used regular expressions" shared by the editor will be helpful to everyone? at the same time, I also hope that you will support and pay attention to the industry information channel. more related knowledge is waiting for you to learn!