Collection of common Regular Expressions and Regular Expressions

Source: Internet
Author: User
Tags uppercase character

Collection of common Regular Expressions and Regular Expressions

Collection of common Regular Expressions

1. verification number: Only one numeric expression can be entered ^ \ d $ to describe an example of matching a digit, 0, 1, 2, and 2 that does not match. you can only enter n numeric expressions ^ \ d {n} $ for example ^ \ d {8} $ to describe the example of matching 8 numbers 12345678,22223334, 12344321 does not match 3. you can only enter at least n numeric expressions ^ \ d {n,} $ for example ^ \ d {8,} $ to describe the example of matching at least n numbers 12345678,123456789, 12344321 mismatched Example 4. only m to n numeric expressions can be input ^ \ d {m, n} $ for example, ^ \ d {123456,123} $ describe an example of matching m to n numbers 12345678,1234567 mismatched 4567895. you can only enter a numeric expression ^ [0-9] * $ to describe the example of matching any number: 12345678,1234567 mismatched example E, clear moon 6. you can only enter a range numeric expression ^ [12-15] $ to describe the example of a number matching a range of 12, 13, 14, 15 do not match Example 7. only 0 and non-0 numeric expressions can be entered ^ (0 | [1-9] [0-9] *) $ the description can be 0, and the first number cannot be 0, there can be 0 matching examples of 101,100, not matching example 01, clear moon, http://blog.csdn.net/21aspnet8 . You can only enter the real expression ^ [-+]? \ D + (\. \ d + )? $ Example 18, + 3.14,-9.90 that matches a real number. 6, 33 s, 67-999. only positive real number expressions with n decimal places can be input ^ [0-9] + (. [0-9] {n })? $ With ^ [0-9] + (. [0-9] {2 })? $ For example, an example of a positive number matching n decimal places is described. Example 2.22 Of a non-matching case 2.222,-2.22, http://blog.csdn.net/21aspnet10 . Only the positive number expression ^ [0-9] + (. [0-9] {m, n}) of m-n decimal places can be entered })? $ With ^ [0-9] + (. [0-9] {1, 2 })? $ For example, 2.22 is used to describe an example of positive number matching m to n decimal places, 2.2 is not used as an example of 2.222,-2.2222, http://blog.csdn.net/21aspnet11 . Only positive integer expressions other than 0 can be entered ^ \ +? [1-9] [0-9] * $ example of a positive integer that matches a non-zero value: 234, 0,-4, 12. only a non-0 negative integer expression ^ \-[1-9] [0-9] * $ can be entered to describe an example of a non-0 negative integer match-2,-23, -234 mismatched examples:, 4, 13. only n character expressions can be entered ^. {n} $ to ^. {4} $ is used as an example to describe how to match n characters. Note that Chinese characters are only counted as 1-character matching examples: 1234, 12we, 123 clear. Examples of non-matching clear moon: 0,123,123 www, http://blog.csdn.net/21aspnet/ 14. You can only enter the English character expression ^. [A-Za-z] + $ as an example to describe an example of English character matching, case-insensitive, Asp, WWW, and mismatched example 0,123,123 www, http://blog.csdn.net/21aspnet/ 15. only uppercase English character expressions can be entered ^. [A-Z] + $ as an example to describe the example of matching English uppercase character matching NET, WWW, mismatched example 0,123,123 www, 16. only lowercase English character expressions can be entered ^. [a-z] + $ is used as an example to describe asp that matches uppercase English characters. Examples of csdn do not match 0, NET, WWW, 17. only English characters and numbers can be entered ^. [A-Za-z0-9] + $ example of matching English characters + digit matching example 1Asp, W1W1W, mismatched example 0,123,123, www, http://blog.csdn.net/21aspnet/ 18. only English characters, numbers, and underlines can be entered. For example, 1Asp, WWW, 12,1_w do not match. Example 3 #, 2-4, w # $, http://blog.csdn.net/21aspnet/ 19. password example expression ^. [a-zA-Z] \ w {m, n} $ the following example does not match the m-n characters that match the English character and can only match numbers, letters, or underscores. 20. verify the first letter uppercase expression \ B [^ \ Wa-z0-9 _] [^ \ WA-Z0-9 _] * \ B description the first letter can only uppercase matching example Asp, Net mismatch example http://blog.csdn.net/21aspnet/ 21. Verify the URL (? Id = Chinese). NET2005 does not have this function expression ^ http: \/([\ w-] + (\. [\ w-] +) + (\/[\ w -. \/\? % & = \ U4e00-\ u9fa5] *)? $ Description verification tape? Id = Chinese matching example http://blog.csdn.net/21aspnet/ , Http://blog.csdn.net? Id = example of non-match between qingyue and qingyue 22. Verify the Chinese character expression ^ [\ u4e00-\ u9fa5] {0 ,}$ describes examples of Chinese Character matching only. http://blog.csdn.net/21aspnet/ 23. Verify the QQ number expression [0-9] {5, 9} to describe an example of a matching QQ number of 5-9 characters. Example of 10000 w that does not match 123456, http://blog.csdn.net/21aspnet/ 24. verify the email (verify the same MSN number) expression \ w + ([-+. '] \ w +) * @ \ w + ([-.] \ w + )*\. \ w + ([-.] \ w +) * description note MSN non-hotmail.com mailbox can also be matched examples of aaa@msn.com mismatch example 111 @ 1. http://blog.csdn.net/21aspnet/ 25. verify the ID card number (for rough verification, it is best to tune the class library on the server side for further verification) expression ^ [1-9] ([0-9] {16} | [0-9] {13 }) [xX0-9] $ describe the matching example of a 15 or 18-digit ID card number, supporting an unmatched example with X http://blog.csdn.net/21aspnet/ 26. verify the mobile phone number (including 159, excluding PHS) expression ^ 13 [0-9] {1} [0-9] {8} | ^ 15 [9] {1} [0-9] {8} The description contains 159 example of mobile phone number 130-139 matching 139 XXXXXXXX mismatched example 140 XXXXXXXX, http://blog.csdn.net/21aspnet/ 27. verify the phone number (complex,. NET2005 gave an incorrect expression (not perfect) solution 1 (\ d {3} \) | \ d {3 }-) | (\ d {4} \) | \ d {4 }-))? (\ D {8} | \ d {7}) solution 2 (^ [0-9] {3, 4} \-[0-9] {3, 8} $) | (^ [0-9] {3, 8} $) | (^ \ ([0-9] {3, 4} \) [0-9] {3, 8} $) | (^ 0 {0, 1} 13 [0-9] {9} $) mobile phone number supported but not perfectly described Shanghai: 02112345678 3 + 8 digits Shanghai: 021-12345678 Shanghai :( 021) -12345678 Shanghai :( 021) 12345678 Zhengzhou: 03711234567 4 + 7 digits Hangzhou: 057112345678 4 + 8 digits with extension number, because the status of country code is very complex, we do not recommend that the front-end do 100% verification. So far, no one seems to be able to write a file containing all types. In fact, there are many situations that are in conflict. If anyone who has a better phone number, please leave a message to match the example that does not match. Example 28. verify passport expression (P \ d {7}) | G \ d {8 }) describe the example that verifies the mismatch between the P + 7 numbers and the G + 8 numbers, http://blog.csdn.net/21aspnet/ 29. verify IP expression ^ (25 [0-5] | 2 [0-4] [0-9] | [0-1] {1} [0-9] {2} | [1-9] {1} [0-9] {1} | [1-9]) \. (25 [0-5] | 2 [0-4] [0-9] | [0-1] {1} [0-9] {2} | [1- 9] {1} [0-9] {1} | [1-9] | 0) \. (25 [0-5] | 2 [0-4] [0-9] | [0-1] {1} [0-9] {2} | [1- 9] {1} [0-9] {1} | [1-9] | 0) \. (25 [0-5] | 2 [0-4] [0-9] | [0-1] {1} [0-9] {2} | [1- 9] {1} [0-9] {1} | [0-9]) $ example of verifying IP match 192.168.0.1 222.234.1.4 does not match 30. verify domain expression ^ [a-zA-Z0-9] + ([a-zA-Z0-9 \-\.] + )? \. S |) $ example of verifying domain match csdn.net baidu.com it.com.cn does not match 192.168.0.1 31. Verify the credit card expression ^ ((?: 4 \ d {3}) | (?: 5 [1-5] \ d {2}) | (?: 6011) | (?: 3 [68] \ d {2}) | (?: 30 [012345] \ d) [-]? (\ D {4}) [-]? (\ D {4}) [-]? (\ D {4} | 3 [] \ d {13}) $ example 32 that describes how to verify that the matching examples of Visa, MasterCard, Discover Card, and American Express Card do not match. verify the ISBN international standard number expression ^ (\ d [-] *) {9} [\ dxX] $ example of verifying ISBN international standard number matching 7-111-19947-2 mismatch 33. verify GUID globally unique identifier expression ^ [A-Z0-9] {8}-[A-Z0-9] {4}-[A-Z0-9] {4}-[A-Z0-9] {4}-[A-Z0-9] {12} $ example of matching format 8-4-4-4-12 2064d355-c0b9-41d8-9ef7-9d8b26524751 mismatch 34. verify the file path and extension expression ^ ([a-zA-Z] \: |\\) \ ([^ \] + \) * [^ \/: *? "<> |] + \. Txt (l )? $ Example E: \ mo.txt which does not match the file extension check path and file extension examples E: \, mo.doc, E: \ mo.doc, http://blog.csdn.net/21aspnet/ 35. Verify the Html color value expression ^ #? ([A-f] | [A-F] | [0-9]) {3} ([a-f] | [A-F] | [0-9]) {3 })? $ Example of color value check # example of FF0000 Mismatch http://blog.csdn.net/21aspnet/ ^ [1-9] \ d *\. \ d * | 0 \. \ d * [1-9] \ d * $ integer or decimal number: ^ [0-9] + \. {0, 1} [0-9] {0, 2} $ can only enter numbers: "^ [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 the User Password: "^ [a-zA-Z] \ w {5, 17} $". The correct format is: start with a letter, with a length of 6 ~ It can only contain characters, numbers, and underscores. Check whether ^ % & ',; =? $ \ "And other characters:" [^ % & ',; =? $ \ X22] + ". Only Chinese characters can be entered: "^ [\ u4e00-\ u9fa5] {0,} $" verification 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 ". Regular Expression matching Chinese characters: [\ u4e00-\ u9fa5] matching 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;} match the regular expression of the blank line: \ n [\ s |] * \ r match the regular expression of the html Tag: <(. *)> (. *) <\/(. *)> | <(. *) \/> Regular Expression matching spaces at the beginning and end: (^ \ s *) | (\ s * $) Application: javascript does not contain trim functions like vbscript, we can use this expression as follows: String. prototype. trim = function () {return this. replace (/(^ \ s *) | (\ s * $)/g, "");} Use a regular expression to break down and convert ip addresses: Below is a Javascript program that uses a regular expression to match ip addresses and convert ip addresses to corresponding values: function IP2V (ip) {re =/(\ d + )\. (\ d + )\. (\ d + )\. (\ d +)/g // The regular expression that matches the IP address if (re. test (ip) {return RegExp. $1 * Math. pow (255, 3) + RegExp. $2 * Math. pow (255, 2) + RegExp. $3*255 + 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 simpler to use the split function directly. 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 )) the regular expression matching the Email address: \ w + ([-+.] \ w +) * @ \ w + ([-.] \ w + )*\. \ w + ([-.] \ w +) * Regular Expression matching the URL: http: // ([\ w-] + \.) + [\ w-] + (/[\ w -. /? % & =] *)? Use regular expressions to restrict text box input in a webpage form: use regular expressions to restrict only Chinese characters: onkeyup = "value = value. replace (/[^ \ u4E00-\ u9FA5]/g, '')" onbeforepaste = "clipboardData. setData ('text', clipboardData. getData ('text '). replace (/[^ \ u4E00-\ u9FA5]/g, '')" use a regular expression to limit that only full-width characters can be entered: onkeyup = "value = value. replace (/[^ \ uFF00-\ uFFFF]/g, '')" onbeforepaste = "clipboardData. setData ('text', clipboardData. getData ('text '). replace (/[^ \ uFF00-\ uFFFF]/g, '')" use a regular expression to limit the number to onke. Yup = "value = value. replace (/[^ \ d]/g, '')" onbeforepaste = "clipboardData. setData ('text', clipboardData. getData ('text '). replace (/[^ \ d]/g, '')" You can only enter numbers and English letters using regular expressions: onkeyup = "value = value. replace (/[\ W]/g, '')" onbeforepaste = "clipboardData. setData ('text', clipboardData. getData ('text '). replace (/[^ \ d]/g, '')" <input onkeyup = "value = value. replace (/[^ \ u4E00-\ u9FA5 \ w]/g, '')" onbeforepaste = "clipboardData. setData ('text', cl IpboardData. getData ('text '). replace (/[^ \ u4E00-\ u9FA5 \ w]/g, '')" value = "underline allowed, "> <script language =" javascript "> if (document. layers) // trigger the keyboard event document. captureEvents (Event. KEYPRESS) function xz (thsv, nob) {if (nob = "2") {window. clipboardData. setData ("text", "") alert ("avoid illegal character input, do not copy character"); return false;} if (event. keyCode! = 8 & event. keyCode! = 16 & event. keyCode! = 37 & event. keyCode! = 38 & event. keyCode! = 39 & event. keyCode! = 40) {thsvv = thsv. value; // The input value thsvs = thsvv. substring (thsvv. length-1); // enter the last character // thsvss = thsvv. substring (0, thsvv. length-1); // remove the last error character if (! Thsvs. replace (/[^ \ u4E00-\ u9FA5 \ w]/g, '') | event. keyCode = 189) {// remove the symbol and underline key thsv from the regular expression. value = 'do not enter invalid symbols [' + thsvs + ']'; alert ('do not enter invalid symbols [' + thsvs + ']'); thsv. value = ""; return false ;}}</script> <input onkeyup = "xz (this, 1)" onPaste = "xz (this, 2) "value =" "> allow numbers, letters, and Chinese characters <script language =" javascript "> <! -- Function MaxLength (field, maxlimit) {var j = field. value. replace (/[^ \ x00-\ xff]/g ,"**"). length; // alert (j); var tempString = field. value; var tt = ""; if (j> maxlimit) {for (var I = 0; I <maxlimit; I ++) {if (tt. replace (/[^ \ x00-\ xff]/g ,"**"). length <maxlimit) tt = tempString. substr (0, I + 1); else break;} if (tt. replace (/[^ \ x00-\ xff]/g ,"**"). length> maxlimit) tt = tt. substr (0, tt. length-1); field. value = tt;} els E {;}</script> single-line text box Control <br/> <INPUT type = "text" id = "Text1" name = "Text1" onpropertychange = "MaxLength (this, 5) "> <br/> multi-line text box control: <br/> <TEXTAREA rows = "14" cols = "39" id = "Textarea1" name = "Textarea1" onpropertychange = "MaxLength (this, 15) "> </TEXTAREA> <br/> You can only enter numbers and Chinese characters in the control form .... <script> function test () {if (document. a. b. value. length> 50) {alert ("cannot exceed 50 characters! "); Document. a. b. focus (); return false ;}}</script> <form name = a onsubmit = "return test () "> <textarea name =" B "cols =" 40 "wrap =" VIRTUAL "rows =" 6 "> </textarea> <input type =" submit "name =" Submit "value =" check "> </form> can only be Chinese characters <input onkeyup =" value = value. replace (/[^ \ u4E00-\ u9FA5]/g, '')"> only English characters <script language = javascript> function onlyEng () {if (! (Event. keyCode> = 65 & event. keyCode <= 90) event. returnValue = false ;}</script> <input onkeydown = "onlyEng (); "> <input name =" coname "type =" text "size =" 50 "maxlength =" 35 "class = input2 onkeyup =" value = value. replace (/[\ W]/g, '')" onbeforepaste = "clipboardData. setData ('text', clipboardData. getData ('text '). replace (/[^ \ d]/g, '')"> only numbers <script language = javascript> function onlyNum () {if (! (Event. keyCode> = 48 & event. keyCode <= 57) | (event. keyCode> = 96 & event. keyCode <= 105) // consider the number key event on the keypad. returnValue = false ;}</script> <input onkeydown = "onlyNum ();"> only English characters and numbers <input onkeyup = "value = value. replace (/[\ W]/g, '')" onbeforepaste = "clipboardData. setData ('text', clipboardData. getData ('text '). replace (/[^ \ d]/g, '')"> verify the email format as <script language = Javascript RUNAT = Server> function isEmail (strEmail) {If (strEmail. search (/^ \ w + (-\ w +) | (\. \ w +) * \ @ [A-Za-z0-9] + ((\. |-) [A-Za-z0-9] + )*\. [A-Za-z0-9] + $ /)! =-1) return true; else alert ("oh") ;}</SCRIPT> <input type = text onblur = isEmail (this. value)> shielded keywords (sex, fuck)-modified <script language = "JavaScript1.2"> function test () {if (. b. value. indexOf ("sex") = 0) | (. b. value. indexOf ("fuck") = 0) {alert ("five lectures, four beautiful three loves");. b. focus (); return false ;}}</script> <form name = a onsubmit = "return test () "> <input type = text name = B> <input type =" submit "name =" Submit "v Alue = "check"> </form>: only numbers <input onkeyup = "if (event. keyCode! = 37 & event. keyCode! = 39) value = value. replace (/\ D/g, '');" onbeforepaste = "clipboardData. setData ('text', clipboardData. getData ('text '). replace (/\ D/g, '')"> mobile phone number :( ^ (\ d {3, 4 }-)? \ D {7, 8}) $ | (13 [0-9] {9}) | (15 [8-9] {9 }) no, you can write it according to the above description. It just takes some time. Verify the number's regular expression set verification number: ^ [0-9] * $ verify the n-digit number: ^ \ d {n} $ verify the number of at least n digits: ^ \ d {n ,}$ verify the m-n digit: ^ \ d {m, n }$ verify the number starting with zero or zero: ^ (0 | [1-9] [0-9] *) $ verify the positive number of two decimal places: ^ [0-9] + (. [0-9] {2 })? $ Verify the positive number of 1-3 decimal places: ^ [0-9] + (. [0-9] {1, 3 })? $ Verify a non-zero positive integer: ^ \ +? [1-9] [0-9] * $ verify a non-zero negative integer: ^ \-[1-9] [0-9] * $ verify a non-negative integer (positive integer + 0) ^ \ d + $ verify a non-positive integer (negative integer + 0) ^ (-\ d +) | (0 +) $ verify a 3-character length: ^. {3} $ verify A string consisting of 26 English letters: ^ [A-Za-z] + $ verify A string consisting of 26 uppercase English letters: ^ [A-Z] + $ verify a string consisting of 26 lower-case English letters: ^ [a-z] + $ verify a string consisting of digits and 26 English letters: ^ [A-Za-z0-9] + $ verify a string consisting of digits, 26 letters, or underscores: ^ \ w + $ verify user password: ^ [a-zA-Z] \ w {5, 17} $ the correct format is: it must start with a letter and be between 6 and 18 characters. It can only contain characters, numbers, and underscores. Check whether ^ % & ',; =? $ \ "And other characters: [^ % & ',; =? $ \ X22] + verify Chinese characters: ^ [\ u4e00-\ u9fa5], {0,} $ verify Email address: ^ \ w + [-+.] \ w +) * @ \ w + ([-.] \ w + )*\. \ w + ([-.] \ w +) * $ verify InternetURL: ^ http: // ([\ w-] + \.) + [\ w-] + (/[\ w -. /? % & =] *)? $; ^ [A-zA-z] +: // (w + (-w +) *) (. (w + (-w + )*))*(? S *)? $ Verification phone number: ^ (\ d {3, 4} \) | \ d {3, 4 }-)? \ D {7,8} $: -- the correct format is: XXXX-XXXXXXX, XXXX-XXXXXXXX, XXX-XXXXXXX, XXX-XXXXXXXX, XXXXXXX, XXXXXXXX. Verify ID card number (15 or 18 digits): ^ \ d {15} | \ d {} 18 $ verify 12 months of a year: ^ (0? [1-9] | 1 [0-2]) $ the correct format is: "01"-"09" and "1" "12" to verify the 31 days of a month: ^ (0? [1-9]) | (1 | 2) [0-9]) | 30 | 31) $ the correct format is: 01, 09, 1, 31. Integer: ^ -? \ D + $ non-negative floating point number (Positive floating point number + 0): ^ \ d + (\. \ d + )? $ Positive floating point ^ ([0-9] + \. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] * \. [0-9] +) | ([0-9] * [1-9] [0-9] *) $ non-Positive floating point number (negative floating point number + 0) ^ (-\ d + (\. \ d + )?) | (0 + (\. 0 + )?)) $ Negative floating point number ^ (-([0-9] + \. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] * \. [0-9] +) | ([0-9] * [1-9] [0-9] *) $ floating point number ^ (-? \ D +) (\. \ d + )?

The above is a common Regular Expression summarized in this article. I hope it will be helpful for you to learn regular expressions.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.