Below are some common regular expressions. If you think it is practical, add them to your favorites.
Only numbers are allowed: "^ [0-9] * $ ".
Only n digits can be entered: "^ d {n} $ ".
You can only enter at least n digits: "^ d {n,} $ ".
Only m ~ can be input ~ N-digit :. "^ D {m, n} $"
Only numbers starting with zero and non-zero can be entered: "^ (0 | [1-9] [0-9] *) $ ".
Only positive numbers with two decimal places can be entered: "^ [0-9] + (. [0-9] {2 })? $ ".
Only 1 ~ Positive number of three decimal places: "^ [0-9] + (. [0-9] {1, 3 })? $ ".
Only a non-zero positive integer can be entered: "^ +? [1-9] [0-9] * $ ".
Only a non-zero negative integer can be entered: "^-[1-9] [] 0-9" * $.
Only 3 characters can be entered: "^. {3} $ ".
You can only enter A string consisting of 26 English letters: "^ [A-Za-z] + $ ".
You can only enter a string consisting of 26 uppercase letters: "^ [A-Z] + $ ".
You can only enter a string consisting of 26 lower-case English letters: "^ [a-z] + $ ".
You can only enter a string consisting of a number and 26 English letters: "^ [A-Za-z0-9] + $ ".
You can only enter a string consisting of digits, 26 English letters, or underscores (_): "^ w + $ ".
Verify User Password: "^ [a-zA-Z] w {5, 17} $" correct format: Start with a letter, Length: 6 ~ It can only contain characters, numbers, and underscores.
Check whether ^ % & ,;=? $ "And other characters:" [^ % &,; =? $ X22] + ".
Only Chinese characters can be entered: "^ [u4e00-u9fa5] {0,} $"
Verify Email address: "^ w + ([-+.] w +) * @ w + ([-.] w + )*. w + ([-.] w +) * $ ".
Verify InternetURL: "^ http: // ([w-] +.) + [w-] + (/[w -./? % & =] *)? $ ".
Verification phone number: "^ (d {3, 4}-) | d {3.4 }-)? D {7,8} $ "correct format:" XXX-XXXXXXX "," XXXX-XXXXXXXX "," XXX-XXXXXXX "," XXX-XXXXXXXX "," XXXXXXX "and" XXXXXXXX ".
Verify the ID card number (15 or 18 digits): "^ d {15} | d {18} $ ".
12 months of verification: "^ (0? [1-9] | 1 [0-2]) $ "the correct format is:" 01 "~ "09" and "1 "~ "12 ".
31 days of verification for a month: "^ (0? [1-9]) | (1 | 2) [0-9]) | 30 | 31) $ "the correct format is;" 01 "~ "09" and "1 "~ "31 ".
Use regular expressions to restrict text box input in a webpage form:
You can only enter Chinese characters using regular expressions:
You can only enter full-width characters using regular expressions:
You can only enter numbers using regular expressions:
You can only enter numbers and English letters using regular expressions:
Javascript programs that extract file names from URLs using regular expressions. the following result is page1.
S = "http://www.janbao.net"
S = s. replace (/(. */) {0,} ([^.] +). */ig, "$2 ")
Alert (s)
Match double byte characters (including Chinese characters): [^ x00-xff]
Application: Calculate the length of a string (two-byte length Meter 2, ASCII character meter 1)
String. prototype. len = function () {return this. replace ([^ x00-xff]/g, "aa"). length ;}
Regular Expression for matching empty rows: [s |] *
Regular Expressions matching HTML tags:/<(. *)>. * </1> | <(. *)/>/
Regular Expression matching the first and last spaces: (^ s *) | (s * $)
String. prototype. trim = function ()
{
Return this. replace (/(^ s *) | (s * $)/g ,"");
}
Use regular expressions to break down and convert IP addresses:
The following is a Javascript program that uses regular expressions to match IP addresses and convert IP addresses to corresponding values:
Function IP2V (ip)
{
Re =/(d +). (d +)/g // Regular Expression matching IP addresses
If (re. test (ip ))
{
Return RegExp. $1 * Math. pow (255) + RegExp. $2 * Math. pow () + RegExp. $3 * + RegExp. $4*1
}
Else
{
Throw new Error ("Not a valid IP address! ")
}
}
However, if the above program does not use regular expressions, it may be easier to directly use the split function to separate them. The program is as follows:
Var ip = "10.100.0000168"
Ip = ip. split (".")
Alert ("the IP value is: "+ (ip [0] * 255*255*255 + ip [1] * 255*255 + ip [2] * 255 + ip [3] * 1 ))
Symbol explanation:
Character Description
Mark the next character as a special character, a literal character, or a backward reference, or an octal escape character.
For example, n matches the character "n ". Match A linefeed. Sequence \ matches "" and "(" matches "(".
^ Matches the start position of the input string. If the Multiline attribute of the RegExp object is set, ^ also matches or is followed.
$ Matches the end position of the input string. If the Multiline attribute of the RegExp object is set, $ also matches or the previous position.
* Matches the previous subexpression zero or multiple times. For example, zo * can match "z" and "zoo ". * Is equivalent to {0 ,}.
+ Match the previous subexpression once or multiple times. For example, zo + can match "zo" and "zoo", but cannot match "z ". + Is equivalent to {1 ,}.
? Match the previous subexpression zero or once. For example, "do (es )? "Can match" do "in" do "or" does ".? It is equivalent to {0, 1 }.
{N} n is a non-negative integer. Match n times. For example, o {2} cannot match the o in "Bob", but can match the two o in "food.
{N,} n is a non-negative integer. Match at least n times. For example, o {2,} cannot match o in "Bob", but can match all o in "foooood. O {1,} is equivalent to o +. O {0,} is equivalent to o *.
Both {n, m} m and n are non-negative integers, where n <= m. Match at least n times and at most m times. For example, "o {1, 3}" matches the first three o in "fooooood. O {0, 1} is equivalent to o ?. Note that there must be no space between a comma and two numbers.
? When this character is followed by any other delimiter (*, + ,?, The matching mode after {n}, {n ,}, {n, m}) is not greedy. The non-Greedy mode matches as few searched strings as possible, while the default greedy mode matches as many searched strings as possible. For example, for strings "oooo", o ++? A single "o" will be matched, while o + will match all o.
. Match any single character. To match any character, use a pattern like.
(Pattern) matches pattern and obtains this match. The obtained match can be obtained from the generated Matches set. The SubMatches set is used in VBScript, and $0… is used in JScript... $9 attribute. To match parentheses, use (OR ).
(? : Pattern) matches pattern but does not get the matching result. That is to say, this is a non-get match and is not stored for future use. This is useful when you use the "or" character (|) to combine each part of a pattern. For example, industr (? : Y | ies) is a simpler expression than industry | industries.
(? = Pattern) Forward pre-query: matches the search string at the beginning of any string that matches pattern. This is a non-get match, that is, the match does not need to be obtained for future use. For example, Windows (? = 95 | 98 | NT | 2000) can match "Windows" in "Windows 2000", but cannot match "Windows" in "Windows 3.1 ". Pre-query does not consume characters, that is, after a match occurs, the next matching search starts immediately after the last match, instead of starting after the pre-query characters.
(?! Pattern) negative pre-query: matches the search string at the beginning of any string that does not match pattern. This is a non-get match, that is, the match does not need to be obtained for future use. For example, Windows (?! 95 | 98 | NT | 2000) can match "Windows" in "Windows 3.1", but cannot match "Windows" in "Windows 2000 ". Pre-query does not consume characters. That is to say, after a match occurs, the next matching search starts immediately after the last match, instead of starting after the pre-query characters.
X | y matches x or y. For example, z | food can match "z" or "food ". (Z | f) matches "zood" or "food ".
[Xyz] Character Set combination. Match any character in it. For example, [abc] can match a in "plain.
[^ Xyz] combination of negative character sets. Match any character not included. For example, [^ abc] can match p in "plain.
[A-z] character range. Matches any character in the specified range. For example, [a-z] can match any lowercase letter in the range of a to z.
[^ A-z] negative character range. Matches any character that is not within the specified range. For example, [^ a-z] can match any character that is not within the range of a to z.
Match A Word boundary, that is, the position between a word and a space. For example, er can match er in "never", but cannot match er in "verb.
B matches non-word boundaries. ErB can match the er in "verb", but cannot match the er in "never.
Cx matches the control characters specified by x. For example, cM matches a Control-M or carriage return character. The value of x must be either a A-Z or a-z. Otherwise, c is treated as a literal c character.
D matches a numeric character. It is equivalent to [0-9].
D. match a non-numeric character. It is equivalent to [^ 0-9].
F matches a form feed. It is equivalent to x0c and cL.
Match A linefeed. Equivalent to x0a