Regular Expressions collection (rpm)

Reprinted from: http: //www.cnblogs.com/guiliangfeng/archive/2010/11/16/1878232.html

  • Enter numbers only: "^ [0-9] * $."
  • Only n-bit digital input: "^ \ d {n} $".
  • Only input digital bits at least n: "^ \ d {n,} $".
  • M ~ n can only enter the number of bits: "^ \ D {m, n} $"
  • You can only enter a zero and non-zero number at the beginning: "^ (0 | [1-9] [0-9] *) $".
  • There are only two positive real number input decimal: "(. [0-9] {2})? ^ [0-9] + $".
  • 1 to enter only three decimal positive real number: "(. [0-9] {1,3})? ^ [0-9] + $".
  • You can only enter a non-zero positive integer: "? ^ \ + [1-9] [0-9] * $."
  • You can only enter a non-zero negative integers: "^ \ - [1-9] [] 0-9" * $.
  • Only 3 of the length of the input characters:. "$ ^ {3}."
  • 26 only by the input string of letters in English: "^ [A-Za-z] + $".
  • You can only enter a string of 26 English capital letters: "^ [AZ] + $".
  • You can only enter letters written by a string consisting of 26 small: "^ [az] + $".
  • Only input string of numbers and English letters 26: "^ [A-Za-z0-9] + $".
  • You can only enter the numbers, 26 English letters or underscore the string: "^ \ w + $".
  • Verify User Password: "^ [a-zA-Z] \ w {5,17} $" The correct format is: begin with a letter, a length between 6 and 18, only contain characters, numbers and underscores.
  • Verify that contain ^% & '?,; = $ \ "Characters such as:" [^% &'?,; = $ \ X22] + ".
  • Can input Chinese characters: "^ [\ u4e00- \ u9fa5] {0,} $"
  • 验证Email地址:"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"。
  • 验证InternetURL:"^http://([/\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$"。
  • Verify that the phone number: "^ (\ (\ d {3,4} -) | \ d {3.4} -) \ d {7,8} $?" The correct format is: "XXX-XXXXXXX", "XXXX-XXXXXXXX "," XXX-XXXXXXX "," XXX-XXXXXXXX "," XXXXXXX "and" XXXXXXXX ".
  • ID verification number (15 or 18 digits): "^ \ d {15} | \ d {18} $".
  • Verify year 12 months: "^ (0 [1-9] | 1 [0-2]?) $" Correct format: "01" through "09" and "1" to "12."
  • Verify month 31 days: "^ (? (0 [1-9]) | ((1 | 2) [0-9]) | 30 | 31) $" correct format; "01" through "09" and "1" to "31." 

Limit the use of regular expressions in web form text input box contents:

As a regular expression can only enter Chinese restrictions: onkeyup = "value = value.replace (/ [^ \ u4E00- \ u9FA5] / g, '')" onbeforepaste = "clipboardData.setData ( 'text', clipboardData.getData ( 'text'). replace (/ [^ \ u4E00- \ u9FA5] / g, '')) "

As a regular expression can limit the full-size character input: onkeyup = "value = value.replace (/ [^ \ uFF00- \ uFFFF] / g, '')" onbeforepaste = "clipboardData.setData ( 'text', clipboardData.getData ( 'text'). replace (/ [^ \ uFF00- \ uFFFF] / g, '')) "

As a regular expression can limit the input digital: onkeyup = "value = value.replace (/ [^ \ d] / g, '')" onbeforepaste = "clipboardData.setData ( 'text', clipboardData.getData ( 'text' ) .replace (/ [^ \ d] / g, '')) "

As a regular expression can only enter numbers and restrictions: onkeyup = "value = value.replace (/ [\ W] / g, '')" onbeforepaste = "clipboardData.setData ( 'text', clipboardData.getData ( 'text ') .replace (/ [^ \ d] / g,' ')) "

Have to use regular expressions to extract the file name from the URL address javascript program, the following results for the page1

s="http://www.9499.net/page1.htm"
s=s.replace(/(.*\/){0,}([^\.]+).*/ig,"$2")
alert(s)

Match double-byte characters (including Chinese characters included): [^ \ x00- \ xff]

Application: calculate the length of the string (a double-byte character length meter 2, ASCII characters are counted 1)

String.prototype.len=function(){return this.replace([^\x00-\xff]/g,"aa").length;}

Blank line matching the regular expression: \ n [\ s |] * \ r

HTML tags matching regular expression:. / <(. *)> * <\ / \ 1> | <(. *) \ /> /

Trailing spaces matching regular expression: (^ \ s *) | (\ s * $)

String.prototype.trim = function()
{
    return this.replace(/(^\s*)|(\s*$)/g, "");
}

Use regular expressions decomposition and conversion IP address:

The following regular expression matching using the IP address, and IP address values ​​into corresponding Javascript program:

function IP2V (IP)
{
 Re = / (\ D +) \. (\ D +) \. (\ D +) \. (\ D +) / G // matches the IP address regular expression
if (re.test (ip) )
{
return the RegExp. $ *. 1 Math.pow (255,3)) + the RegExp. $ 2 * Math.pow (255,2)) + the RegExp. $. 3 * 255 + the RegExp. $ *. 1. 4
}
the else
{
 the throw new new Error ( " A Valid the IP address Not! ")
}
}

However, if the above procedures do not have a regular expression, while the direct use of split function to break down may be more simple procedure is as follows:

IP = var "10.100.20.168"
IP = ip.split ( ".")
Alert ( "the IP value is:" + (ip [0] * 255 * 255 * 255 + ip [1] * 255 * 255 + ip [ 2] * 255 + ip [3 ] * 1))
explanation of symbols:

Character 
Description


The next character is marked as a special character, or a literal character, or a backward reference, or an octal escape. For example, 'n' matches the character "n". '\ n' matches a newline. Sequence '\\' matches "\" and "\ (" the match "(."


Matches the beginning of the string. If the object is set RegExp Multiline property, also matches ^ '\ n' position after or '\ r'.


Matches the input end of the string. If the object is set RegExp Multiline property, $ also matches the position before the '\ 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 +' will match "zo" and "zoo", but can not match the "z". + Is equivalent to {1}.


Matches the preceding subexpression zero or one. For example, "do (es)?" Matches "do" or "does" in the "do". ? Is equivalent to {0,1}.

n-} { 
n-is a non-negative integer. Matching the determined n times. For example, 'o {2}' does not match the "Bob" in the 'o', but can match the "food" in the two o.

n-{,} 
n-it is a non-negative integer. Matching at least n times. For example, 'o {2,}' does not match the "Bob" in the 'o', but it can match all o "foooood" in. 'o {1,}' is equivalent to 'o +'. 'o {0,}' is equivalent to 'o *'.

n {, m} 
m and n are non-negative integers, where n <= m. Match at least n times and match up to m times. For example, "o {1,3}" will match "fooooood" in the previous three o. 'o {0,1}' is equivalent to 'o?'. Please note that no spaces between the comma and the two numbers.


When the characters followed in any other qualifier (*, +,?, { N}, {n,}, {n, m}) when the rear, non-greedy matching pattern. Non-greedy pattern matches as little as possible the search string, and the default greedy pattern matches as much of the string search. For example, the string "oooo", 'o +? ' Matches a single "o", and 'o +' will match all 'o'.


Matches any single character except "\ n" is. To match include '\ n', including any character, like the use of '[. \ N]' mode.

(pattern) 
matches the pattern and get the match. The matching can be obtained from the Matches have been used in collection SubMatches VBScript, JScript is used in the $ 0 ... $ 9 properties. To match parentheses characters, use '\ (' or '\)'.

(:? pattern) 
matches the pattern but do not get matching results, that this is a non-access match, not stored for later use. This use of "or" character (|) to combine the various parts of a model is useful. For example, 'industr (:? Y | ies) is a ratio of' industry | more brief expressions industries'.

(? = pattern) 
Positive pre-investigation, matching the search string at the beginning of the string any pattern matching. This is a non-access match, that is, the match does not need to obtain for later use. For example, 'Windows (= 95 |? 98 | NT | 2000)' can match the "Windows 2000" in the "Windows", but can not match the "Windows 3.1" "Windows". Pre-check does not consume characters, that is, after a match occurs, the last match after the next match to start the search immediately, rather than starting from the characters that contains pre-investigation.

(?! pattern) 
Negative pre-investigation, at the beginning of any match pattern string matching search string. This is a non-access match, that is, the match does not need to obtain for later use. For example 'Windows (95 |?! 98 | NT | 2000)' can match the "Windows 3.1" "Windows", but can not match the "Windows 2000" in the "Windows". Pre-check does not consume characters, that is, after a match occurs, after the last match started immediately next match search, rather than starting from the characters that contains pre-investigation

x | y 
Matches either x or y. For example, 'z | food' can match the "z" or "food". '(z | f) ood' the match "zood" or "food".

[xyz] 
set of characters. Matches any character included. For example, '[abc]' matches "plain" in the 'a'.

[^ xyz] 
negative set of characters. Matches any character not included. For example, '[^ abc]' matches "plain" the 'p'.

[az] 
character range. Matches any character within the specified range. For example, '[az]' match 'a' to any lowercase alphabetic characters 'z' within range.

[^ az] 
Negative character range. Matches any character not in any specified range. For example, '[^ az]' can not match any 'a' to an arbitrary character 'z' within range.

\ b 
matches a word boundary, that is, it refers to the location and spaces between words. For example, 'er \ b' matches "never" in the 'er', but does not match the "verb" in the 'er'.

\ B 
matches non-word boundary. 'er \ B' matches "verb" in the 'er', but does not match the "never" in the 'er'.

\ cx 
matches control characters specified by the x. For example, \ cM matches a Control-M or carriage return. The value of x must be AZ or az. Otherwise, c as a literal 'c' character.

\ d 
Matches a digit character. Equivalent to [0-9].

\ D 
Matches a non-numeric characters. It is equivalent to [^ 0-9].

\ f 
match for a website page. Equivalent to \ x0c and \ cL.

\ n 
Matches a newline. Equivalent to \ x0a and \ cJ.

\ r 
match a carriage return. Equivalent to \ x0d and \ cM.

\ s 
Matches any whitespace characters, including spaces, tabs, page breaks, and so on. Is equivalent to [\ f \ n \ r \ t \ v].

\ S 
Matches any non-whitespace characters. Is equivalent to [^ \ f \ n \ r \ t \ v].

\ t 
matches a tab. Equivalent to \ x09 and \ cI.

\ v 
matches a vertical tab. Equivalent to \ x0b and \ cK.

\ w 
matches any word character including underscore. It is equivalent to the '[A-Za-z0-9_] '.

\ W 
matches any non-word character. It is equivalent to the '[^ A-Za-z0-9_ ]'.

\ xn 
Matches n, where n is a hexadecimal escape value. Hexadecimal escape values must be determined by two digits long. For example, '\ x41' match "A". '\ x041' is equivalent to '\ x04' & "1" . Regular expressions can be used in ASCII encoding. .

\ num 
match num, where num is a positive integer. A reference to the acquired match. For example, '(.) \ 1' matches two consecutive identical characters.

\ n 
identifies an octal escape value or a backward reference. If before \ n at least n captured subexpressions, n is a reference back. Otherwise, if n is an octal digit (0-7), then n is an octal escape value.

\ nm 
identifies an octal escape value or a backward reference. If you have to obtain a sub-expressions nm at least before nm \, then nm is a reference back. If \ There are at least n before obtaining nm, then n is a backward reference character m in the heel. When these conditions are not met, if n and m are octal digits (0-7), \ nm matches octal escape value nm.

\ NML 
If n is an octal digit (0-3), and m and l are octal digits (0-7), the matching octal escape value nml.

\ un 
Matches n, where n is a Unicode character with four hexadecimal digits represented. For example, \ u00A9 matching copyright symbol (?).

Reproduced in: https: //www.cnblogs.com/JoannaQ/archive/2013/03/13/2956915.html

Guess you like

Origin blog.csdn.net/weixin_33672400/article/details/93055943