Regular expressions, a very old and powerful text processing tools, in just a very brief expression statement, will be able to quickly implement a very complex business logic. Mastered regular expressions, you can make your development efficiency greatly improved.
Regular expressions are often used to check or any string fields, such as date formats substantially below this check JavaScript code:
REG = var / ^ (\ D {l, 4}) (- | \ /) (\ D {1,2}) \ 2 (\ D {1,2}) $ /;
var = R & lt fieldValue.match ( REG);
IF (R & lt == null) Alert ( 'the format a Date error!');
frequently used in the development of the front end 20 in the regular expression.
1. Verification password strength
strength of the password must contain a combination of uppercase and lowercase letters and numbers, special characters can not be used, a length of between 8-10.
^(?=.\d)(?=.[a-z])(?=.*[A-Z]).{8,10}$
- Chinese check
string only is Chinese.
^[\u4e00-\u9fa5]{0,}$
- String of digits, letters or underscore 26 composed
^ \ w + $ - Check E-Mail address
with the same password, here's E-mail address regular compliance checks statement.
[\w!#$%&'*+/=?^_{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_
{|}~-]+)*@(?:\w?\.)+\w?
- Check identity card number
below is a regular check identity card number. 15 or 18.
15th place:
^ [1-9] \ d {7 } ((0 \ d) | (1 [0-2])) (([0 | 1 | 2] \ d) | 3 [0-1]) \ d { 3} $
18:
^[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)$
- Calibration date
"yyyy-mm-dd" format check date has been considered flat leap years.
^(?:(?!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)$
- Amount check
sum verification, accurate to two decimal places.
^[0-9]+(.[0-9]{2})?$
- Check the phone number
below 13, 15 is the beginning of the phone number regular expression. (Can be extended according to the first two numbers at the beginning of 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}$
- Judgment IE version
IE yet to be completely replaced, still need to do a lot of pages compatible version, the following expression is IE version check.
^.MSIE 5-8?(?!.Trident\/[5-9]\.0).*$
- Check IP-v4 address
IP4 regular statements.
\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
- Check IP-v6 address
IP6 regular statements.
(([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 URL prefix
application development is often necessary to distinguish between requests HTTPS or HTTP, a prefix may be removed and then the url logical judgment by the following expression.
if (!s.match(/^[a-zA-Z]+:\/\//))
{
s = 'http://' + s;
}
- Extract URL link
following this expression can be selected piece of text in the URL.
^(f|ht){1}(tp|tps):\/\/([\w-]+\.)+[\w-]+(\/[\w- ./?%&=]*)?
- Checking the file path name extension and
the extension of the file path validation and Windows (the example below as a .txt file)
^([a-zA-Z]\:|\\)\\([^\\\\]+\\)*[^\\/:*?"<>|]+\.txt(l)?$
- Color Hex Codes extraction
may need to extract color code page, the following expression may be used.
# ^ ([A-Fa-f0-9] {6} | [A-Fa-f0-9] {3}) $
- Extract Web Images
if you want to extract all the image info pages, you can use the following expression.
\< [img][^\\\\>][src] = [\"\']{0,1}([^\"\'\ >]*)
- Extract pages hyperlinks
Extract hyperlinks in html.
(<a\s(?!.\brel=)[^>])(href="https?:\/\/)((?!(?:(?:www\.)?'.implode('|(?:www\.)?', $follow_list).'))[^"]+)"((?!.\brel=)[^>])(?:[^>])>
- Find CSS properties
by the following expression, you can search to match the CSS properties.
^ \ s [a-zA-Z \ -] + \ s [:] {1} \ s [a-zA- Z0-9 \ s. #] + [;] { 1}
- Extraction Notes
If you need to remove the comment in HMTL, you can use the following expression.
- HTML tags match
by the following expression can match the properties of the HTML tags.
<\/?\w+((\s+\w+(\s=\s(?:".?"|'.?'|[\^'">\s]+))?)+\s|\s)\/?>