Regular expression Instances

Source: Internet
Author: User
1. Verify numbers: Only 1 numeric expressions can be entered ^\d$ description matches an example of a number match 0,1,2,3 mismatch example 2. You can only enter n numeric expressions ^\d{n}$ For example ^\d{8}$ describe matching 8 numbers matching example 12345678,22223 Example of 334,12344321 mismatch 3. You can only enter at least n numeric expressions ^\d{n,}$ For example, ^\d{8,}$ describes an example that matches a minimum of n number matches 12345678,123456789,12344321 a mismatch example 4. Only the number of M to n can be entered The word expression ^\d{m,n}$ For example ^\d{7,8}$ describes an example of matching m to n numbers matching an example of 12345678,1234567 mismatch 123456,1234567895. You can only enter a numeric expression ^[0-9]*$ describe a match to any number Example 12345678,1234567 mismatch example E, clear Moon, Http://blog.csdn.net/21aspnet6. You can only enter an interval numeric expression ^[12-15]$ Describe an example of a number match that matches an interval Example of 12,13,14,15 mismatch 7. Enter only 0 and non-0 numeric expressions ^ (0|[ 1-9][0-9]*) $ description can be 0, the first number cannot be 0, the number can have 0 matching examples 12,10,101,100 mismatch example 01, clear Moon, http://blog.csdn.net/21aspnet8. Only real expressions can be entered ^[ -+]?\d+ (\.\d+)? $ Describes an example that matches a real number match 18,+3.14,-9.90 a mismatch example. 6,33s,67-999. A positive real-number expression ^[0-9]+ (. [) that can only enter N decimal places. 0-9]{n})? $ with ^[0-9]+ (. [ 0-9]{2})? $ For example describes an example of a positive real number matching n decimal digits 2.22 mismatch example 2.222,-2.22,http://blog.csdn.net/21aspnet10. A positive real-number expression ^[0-9]+ (.) that can only enter M-n decimal places. 0-9]{m,n})? $ with ^[0-9]+ (. [ 0-9]{1,2})? $ as an example of an example that describes a positive real number matching m to n decimal places 2.22,2.2 An example of a mismatch 2.222,-2.2222,http://blog.csdn.net/21aspnet11. You can only enter a positive integer that is not 0Number expression ^\+? [1-9] [0-9]*$ Description matches An example of a non-0 positive integer match 2,23,234 example 0,-4,12. You can only enter a negative integer expression that is not 0, ^\-[1-9][0-9]*$ describes an example that matches a negative integer matching non 0 -2,-23,-234 example of a mismatch 0, 4, 13. You can enter only n character expressions ^. {n}$ with ^. {4}$ is an example of a match n characters, note that Chinese characters only count 1 characters to match the example of 1234,12we,123 Qing, qingqing Moon mismatch example 0,123,123WWW,HTTP://BLOG.CSDN.NET/21ASPNET/14. You can only enter English characters An expression ^. [a-za-z]+$, for example, describes matching English characters, case asp,www, case-matching example, 0,123,123WWW,HTTP://BLOG.CSDN.NET/21ASPNET/15. You can only enter uppercase English character expressions ^. [a-z]+$, for example, describes an example of matching English uppercase characters net,www, a mismatch of example 0,123,123www,16. You can only enter lowercase English character expressions ^. [a-z]+$ For example describes an example of matching English uppercase characters asp,csdn mismatch example 0,net,www,17. You can only enter English characters + numeric expressions ^. [a-za-z0-9]+$ as an example of matching English characters + number matching example 1ASP,W1W1W, mismatch example 0,123,123,WWW,HTTP://BLOG.CSDN.NET/21ASPNET/18. Only English characters/numbers/underscores can be entered An example of an expression ^\w+$ an example that matches an English character or a number or underscore match 1asp,www,12,1_w a mismatch of examples 3#,2-4,w#$,http://blog.csdn.net/21aspnet/19. password example expression ^. [a-za-z]\w{m,n}$ Describes an example that matches an m-n-bit character at the beginning of an English character and can only be matched by a numeric letter or underscore 20. Verifying an initial uppercase expression \b[^\wa-z0-9_][^\wa-z0-9_]*\b An example that describes the first letter can only be capitalized Examples of sub-asp,net mismatch http://blog.csdn.net/21aspnet/21. Verify the URL (with id= Chinese) vs.net2005 no this feature expression ^http:\/\/([\w-]+(\. [\w-]+] + (\/[\w-. \/\?%&=\u4e00-\u9fa5]*)?)? $ Description Validation band? Examples of id= in Chinese matches example of Http://blog.csdn.net/21aspnet/,http://blog.csdn.net?id= Qing Moon mismatch 22. Verifying Chinese character expression ^[\u4e00-\u9fa5]{0 ,}$ describes only the example of Chinese character matching to clear the example of HTTP://BLOG.CSDN.NET/21ASPNET/23. Verify that the QQ number expression [0-9]{5,9} describes 5-9-bit QQ number matching example 10000,123456 mismatch example 10000w,http://blog.csdn.net/21aspnet/24. Verifying e-mail (same as verifying MSN number) expression \w+ ([-+. '] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.]     \w+) * Description Note MSN with non-Hotmail.com mailbox can also match example aaa@msn.com mismatch example 111@1. HTTP://BLOG.CSDN.NET/21ASPNET/25. Verifying the identity card number (rough, best server-side tuning Class library fine-grained authentication) expression ^[1-9] ([0-9]{16}|[ 0-9]{13}) [xx0-9]$ describes a matching example 15 or 18-bit ID number that supports an example of a mismatch with X HTTP://BLOG.CSDN.NET/21ASPNET/26. Verify that the phone number (contains 159, does not contain PHS) expression ^13[0-9 ]{1}[0-9]{8}|^15[9]{1}[0-9]{8} Describes an example of a 139XXXXXXXX mismatch that contains a 159 phone number 130-139 match examples 140xxxxxxxx,http://blog.csdn.net/ 21aspnet/27. Verifying the phone number (very complex, VS.) NET2005 given is wrong) expression (imperfect) scheme one ((\ (\d{3}\) |\d{3}-) | ( \ (\d{4}\) |\d{4}-))? (\d{8}|\d{7}) Scenario II (^[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}$) Support cell phone number but not perfect description Shanghai: 02112345678 3+8 bit Shanghai: 021-12345678 Shanghai: (021)-12345678 Shanghai: (021) 12345678 Zhengzhou: 03711234567 4+7-bit hangzhou: 057112345678 4+8-bit and with points Machine number, the situation of the country code because of the situation is very complex so do not recommend the front desk to do 100% verification, so far seems to have no one to write a containing all types, in fact, there are many situations in itself is contradictory. If anyone has a better verification call please leave a message matching example mismatch example 28. Verify Passport expression (P\d{7}) | G\D{8}) Describes examples that verify p+7 numbers and g+8 numbers match the example of a mismatch, http://blog.csdn.net/21aspnet/29. Verify the 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]) $ description Verify IP Match example 192.168.0.1 222.234.1.4 mismatch Example 30. Verify the domain expression ^[a-za-z0-9]+ ([a-za-z0-9\-\.] +)? \.s|) $ Description Validation Domain Match example csdn.net baidu.com it.com.cn mismatch Example 192.168.0.1 31. Verify 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[4,7]\d{13}) $ Description Verify Visa Card, MasterCard, Discover Card, American Express match example mismatch example 32. Verifying ISBN international ISBN expression ^ (\d[-]*) {9}[\dxx]$ Description Verification ISBN international Standard Example of a quasi-ISBN match 7Example of -111-19947-2 mismatch 33. Verify GUID Global Unique identifier expression ^[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}$ description Example of format 8-4-4-4-12 match example of 2064d355-c0b9-41d8-9ef7-9d8b26524751 mismatch 34. Verify the file path and extension expression ^ ([a-za-z]\:|\\) \ \ ([^\\]+\\) *[^\/:* ?" <>  |] +\.txt (L)? $ description Check path and file name extension matching example E:\mo.txt mismatch example E:\, Mo.doc, E:\mo.doc, http://blog.csdn.net/21aspnet/35. Validating HTML color value expressions ^ #? ([a-f]| [a-f]| [0-9]) {3} ([a-f]| [a-f]| [0-9]) {3})? $ description Check Color Match example #FF0000 mismatch example http://blog.csdn.net/21aspnet/^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$ integer or decimal: ^[0-9]+\. {0,1} [0-9] {0,2}$ can only enter a number: "^[0-9]*$". Only n digits can be entered: "^\d{n}$". Only numbers with at least n digits can be entered: "^\d{n,}$". You can enter only the digits of the m~n bit:. "^\d{m,n}$" can only enter numbers starting with 0 and non-0: "^ (0|[ 1-9][0-9]*) $ ". You can only enter a positive real number with two decimal places: "^[0-9]+ (. [ 0-9]{2})? $ ". You can only enter a positive real number with a decimal position: "^[0-9]+ (. [ 0-9]{1,3})? $ ". You can only enter a non-zero positive integer: "^\+?" [1-9] [0-9]*$]. You can only enter a non-zero negative integer: "^\-[1-9][]0-9" *$. Only characters with a length of 3 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 English letters: "^[a-z]+$". You can only enter a string consisting of 26 lowercase 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 a number, 26 letters, or underscores: "^\w+$". Verify user password: "^[a-za-z]\w{5,17}$" is in the correct format: start with a letter, length between 6~18, can contain only characters, numbers, and underscores. Verify that it contains ^%& ',; =?$\ ' 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-./?%&=]*)? $ ". Verify 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 ". Verify the Social Security number (15-bit or 18-digit number): "^\d{15}|\d{18}$". Validation 12 months of the year: "^ (0?[ 1-9]|1[0-2]) $ "The correct format is:" 01 "~" 09 "and" 1 "~" 12 ". Verify one months of 31 days: "^ ((0?[ 1-9]) | ((1|2) [0-9]) |30|31) $ "correct format for;" 01 "~" 09 "and" 1 "~" 31 ". Regular expression matching Chinese characters: [\u4e00-\u9fa5] matches double-byte characters (including kanji): [^\x00-\xff] Apply: Calculates the length of a string (a double-byte character length meter 2,ascii character 1) String.prototype.len =function () {return this.replace (/[^\x00-\xff]/g, "AA"). length; Regular expression that matches a blank line: \n[\s|] *\r regular expressions that match HTML tags:<(.*)>(.*)<\/(.*)>|<(.*)\/>Regular expression matching the leading and trailing spaces: (^\s*) | (\s*$) Application: JavaScript does not have a trim function like VBScript, which we can do with this expression, as follows: String.prototype.trim = function () {return This.replace (/(^\s*) | ( \s*$)/g, "");} Use regular expressions to decompose and convert IP addresses: The following is a JavaScript program that matches an IP address with a regular expression and translates an IP address into a corresponding value: function IP2V (IP) {re=/(\d+) \. \d+) \. (\d+) \. (\d+)/g//match the regular expression of 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, the above program without regular expression, and directly with the split function decomposition may be simpler, the program is as follows: Var ip= "10.100.20.168" Ip=ip.split (".") Alert ("IP value is:" + (ip[0]*255*255* 255+ip[1]*255*255+ip[2]*255+ip[3]*1)) matches the regular expression of the email address: \w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) * Regular expression matching URL URL:/http ([\w-]+\.) +[\w-]+ (/[\w-./?%&=]*)? Use regular expressions to restrict the entry of text boxes in a Web form: Use regular expressions to restrict input to only Chinese: use regular expressions to restrict input of only full-width characters: limit only numbers with regular expressions: You can only enter numbers and English with regular expressions:<inputonkeyup= "Value=value.replace (/[^\u4e00-\u9fa5\w]/g, ')"Onbeforepaste= "Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text ')." Replace (/[^\u4e00-\u9fa5\w]/g, ")"value= "Allow underline, alphanumeric alphabet and kanji"><Scriptlanguage= "JavaScript">if(document.layers)//Trigger Keyboard Eventsdocument.captureevents (event.keypress)functionXZ (thsv,nob) {if(Nob=="2") {Window.clipboardData.setData ("text","") Alert ("avoid illegal character input, do not copy characters"); returnfalse; } if(Event.keycode!=8&&Event.keycode!= -&&Event.keycode!=Panax Notoginseng&&Event.keycode!= -&&Event.keycode!= the&&Event.keycode!= +) {THSVV=Thsv.value;//the value enteredThsvs=thsvv.substring (Thsvv.length-1);//the last character entered//thsvss=thsvv.substring (0,thsvv.length-1);//Remove the last error characterif (!Thsvs.replace (/[^\u4e00-\u9fa5\w]/G,'') ||Event.keycode==189){//regular drop symbols and underscore keyThsv.value='do not enter illegal symbols ['+Thsvs+']'; alert ('do not enter illegal symbols ['+Thsvs+']'); Thsv.value=""; returnfalse; } } } 
  Script><inputonkeyup= "XZ (this,1)"Onpaste= "XZ (this,2)"value="">allow digital letters and kanji<Scriptlanguage= "JavaScript">
   
Related Article

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.