Regex Character | Description |
---|---|
\ | Escapes the next character, marking it as a special character, a literal character, a backreference, or an octal escape. For example, "n " matches the character "n ". "\n " matches a newline character. The string "\\ " matches "\ ", while "\( " matches "( ". |
^ | Matches the beginning of the input string. If the Multiline property of the RegExp object is set, ^ also matches positions after "\n " or "\r ". |
$ | Matches the end of the input string. If the Multiline property of the RegExp object is set, $ also matches positions before "\n " or "\r ". |
* | Matches the preceding subexpression zero or more times. For example, "zo*" matches "z " and "zoo ". * is equivalent to {0,}. |
+ | Matches the preceding subexpression one or more times. For example, "zo+ " matches "zo " and "zoo ", but not "z ". + is equivalent to {1,}. |
? | Matches the preceding subexpression zero or one time. For example, "do(es)? " can match "does " or "do " in "does ". ? is equivalent to {0,1}. |
{n} | n is a non-negative integer. Matches exactly n times. For example, "o{2} " cannot match the "o " in "Bob ", but can match the two o's in "food ". |
{n,} | n is a non-negative integer. Matches at least n times. For example, "o{2,} " cannot match the "o " in "Bob ", but can match all o's in "foooood ". "o{1,} " is equivalent to "o+ ". "o{0,} " is equivalent to "o* ". |
{n,m} | m and n are non-negative integers, with n ≤ m. Matches at least n times and at most m times. For example, "o{1,3} " will match the first three o's in "fooooood ". "o{0,1} " is equivalent to "o? ". Note that there should be no space between the comma and the two numbers. |
? | When this character follows any other quantifier (*, +, ?, {n}, {n,}, {n,m}), the matching mode is non-greedy. Non-greedy mode matches as few characters as possible, while the default greedy mode matches as many as possible. For example, for the string "oooo ", "o+? " matches a single "o ", while "o+ " matches all "o " characters. |
. | Matches any single character except for "\ n ". To match any character including "\ n ", use a pattern like "(.|\n) ". |
(pattern) | Matches pattern and captures this match. The captured match can be obtained from the resulting Matches collection, using the SubMatches collection in VBScript and the $0…$9 properties in JScript. To match parentheses characters, use "\( " or "\) ". |
(?:pattern) | Matches pattern but does not capture the match, meaning it is a non-capturing match and will not be stored for later use. This is useful when using the alternation operator "(|) " to combine different parts of a pattern. For example, "industr(?:y|ies) " is a more concise expression than "industry|industries ". |
(?=pattern) | Positive lookahead, matches the search string at the start of any string that matches pattern. This is a non-capturing match, meaning this match does not need to be stored for later use. For example, "Windows(?=95|98|NT|2000) " matches "Windows " in "Windows2000 ", but not "Windows " in "Windows3.1 ". Lookaheads do not consume characters, meaning that after a match occurs, the next search for a match begins immediately after the last match, not from the character that contains the lookahead. |
(?!pattern) | Negative lookahead, matches the search string at the start of any string that does not match pattern. This is a non-capturing match, meaning this match does not need to be stored for later use. For example, "Windows(?!95|98|NT|2000) " matches "Windows " in "Windows3.1 ", but not "Windows " in "Windows2000 ". Lookaheads do not consume characters, meaning that after a match occurs, the next search for a match begins immediately after the last match, not from the character that contains the lookahead. |
(?<=pattern) | Positive lookbehind, similar to positive lookahead but in the opposite direction. For example, "(?<=95|98|NT|2000)Windows " matches "Windows " in "2000Windows ", but not in "3.1Windows ". |
(?<!pattern) | Negative lookbehind, similar to negative lookahead but in the opposite direction. For example, "(?<!95|98|NT|2000)Windows " matches "Windows " in "3.1Windows ", but not in "2000Windows ". |
x|y | Matches x or y. For example, "z|food " matches "z " or "food ". "(z|f)ood " matches "zood " or "food ". |
[xyz] | Character set. Matches any one of the included characters. For example, "[abc] " matches "a ", "b ", or "c ". |
[^xyz] | Negated character set. Matches any character not included in the brackets. For example, "[^abc] " matches any character except "a ", "b ", or "c ". |
[a-z] | Character range. Matches any character in the specified range. For example, "[a-z] " matches any lowercase letter. You can also combine ranges, such as "[a-zA-Z] ". |
\b | Matches a word boundary, which is the position between a word character (as defined by the regex engine) and a non-word character. For example, "\bword\b " matches "word " but not "worded ". |
\B | Matches a position that is not a word boundary. For example, "\Bword\B " matches "worded " but not "word ". |
\n | Matches the newline character. Different platforms may use different characters for newlines, such as "\r\n " or "\r ". |
\t | Matches the tab character. |
\v | Matches a vertical tab. |
\f | Matches a form feed character. |
\r | Matches a carriage return character. |
\s | Matches any whitespace character, including space, tab, vertical tab, newline, and carriage return characters. It is equivalent to the character class "[ \t\n\x0B\f\r] ". |
\S | Matches any non-whitespace character. It is equivalent to the negated character class "[\s] ". |
\d | Matches any digit character, equivalent to the character class "[0-9] ". |
\D | Matches any non-digit character, equivalent to the negated character class "[\d] ". |
\w | Matches any word character (a-z, A-Z, 0-9, or underscore), equivalent to the character class "[a-zA-Z0-9_] ". |
\W | Matches any non-word character, equivalent to the negated character class "[\w] ". |
Username | /^[a-z0-9_-]{3,16}$/ |
---|---|
Password | /^[a-z0-9_-]{6,18}$/ |
Password 2 | (?=^.{8,}$)(?=.*\d)(?=.*\W+)(?=.*[A-Z])(?=.*[a-z])(?!.*\n).*$ (Must contain digits, uppercase letters, lowercase letters, and punctuation, at least 8 characters) |
Hexadecimal Value | /^#?([a-f0-9]{6}|[a-f0-9]{3})$/ |
/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/ /^[a-z\d]+(\.[a-z\d]+)*@([\da-z](-[\da-z])?)+(\.{1,2}[a-z]+)+$/ or \w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)* | |
URL | /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/ or [a-zA-z]+://[^\s]* |
IP Address | /((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?) /^(?:(?: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]?)$/ or ((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?) |
HTML Tag | /^<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)$/ or <(.*)(.*)>.*<\/\1>|<(.*) \/> |
Remove Code\\Comments | (?<!http:|\S)//.*$ |
Match Double-byte Characters (including Chinese) | [^\x00-\xff] |
Chinese Characters | [\u4e00-\u9fa5] |
Range of Chinese Characters in Unicode | /^[\u2E80-\u9FFF]+$/ |
Chinese and Full-width Punctuation Characters | [\u3000-\u301e\ufe10-\ufe19\ufe30-\ufe44\ufe50-\ufe6b\uff01-\uffee] |
Date (YYYY-MM-DD) | (\d{4}|\d{2})-((0?([1-9]))|(1[1|2]))-((0?[1-9])|([12]([1-9]))|(3[0|1])) |
Date (MM/DD/YYYY) | ((0?[1-9]{1})|(1[1|2]))/(0?[1-9]|([12][1-9])|(3[0|1]))/(\d{4}|\d{2}) |
Time (HH:MM, 24-hour format) | ((1|0?)[0-9]|2[0-3]):([0-5][0-9]) |
Landline Phone Number in Mainland China | (\d{4}-|\d{3}-)?(\d{8}|\d{7}) |
Mobile Phone Number in Mainland China | 1\d{10} |
Postal Code in Mainland China | [1-9]\d{5} |
ID Number in Mainland China (15 or 18 digits) | \d{15}(\d\d[0-9xX])? |
Non-negative Integer (positive integer or zero) | \d+ |
Positive Integer | [0-9]*[1-9][0-9]* |
Negative Integer | -[0-9]*[1-9][0-9]* |
Integer | -?\d+ |
Decimal | (-?\d+)(\.\d+)? |
Blank Line | \n\s*\r or \n\n (editplus) or ^[\s\S ]*\n |
QQ Number | [1-9]\d{4,} |
Words that do not contain 'abc' | \b((?!abc)\w)+\b |
Match Leading and Trailing Whitespace Characters | ^\s*|\s*$ |
Common Editing | Below are some replacements for special Chinese characters (editplus) ^[0-9].*\n ^[^第].*\n ^[习题].*\n ^[\s\S ]*\n ^[0-9]*\. ^[\s\S ]*\n <p[^<>*]> href="javascript:if\(confirm\('(.*?)'\)\)window\.location='(.*?)'" <span style=".[^"]*rgb\(255,255,255\)">.[^<>]*</span> <DIV class=xs0>[\s\S]*?</DIV> |
The regular expression syntax is a quick reference for commonly used regex patterns, syntax inquiries, and provides basic grammar, subexpression syntax, modifiers, greedy and non-greedy modes, allowing for efficient control over strings.