JS Common Regular

Source: Internet
Author: User

2016-09-09

integer or decimal:^[0-9]+\. {0,1} [0-9] {0,2}$ can only enter numbers:"^[0-9]*$". You can only enter numbers with n digits:"^\d{n}$". Only numbers with at least n digits can be entered:"^\d{n,}$". Can only enter M~number of n digits:. "^\d{m,n}$"only numbers starting with 0 and non-0 can be entered:"^ (0| [1-9] [0-9]*) $ ". Only positive reals with two decimal places can be entered:"^[0-9]+ (. [ 0-9]{2})? $ ". You can only enter aThepositive real numbers for decimal places:"^[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" *. You can only enter a length of3the characters:"^. {3}$ ". Only input by26A string consisting of an English alphabet:"^[a-za-z]+$". Only input by26A string consisting of an uppercase English letter:"^[a-z]+$". Only input by26a string consisting of lowercase English letters:"^[a-z]+$". Can only be entered by numbers and26A string consisting of an English alphabet:"^[a-za-z0-9]+$". You can enter only the numbers,26A string consisting of an English letter or an underscore:"^\w+$". To verify the user password:"^[a-za-z]\w{5,17}$"The correct format is: Start with a letter, length in6~18, only characters, numbers, and underscores can be included. Verify that you contain^%& ',; =?$\ "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-./?%&=]*)? $ ". Verify 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 the"XXXXXXXX". Verify the ID number (15bit or18digits):"^\d{15}|\d{18}$". Validate a year's12a month:"^ (0? [1-9]|1[0-2]) $ "the correct format is:"01"~"09"and the"1"~"12". Verify that the one-month31Days:"^ ((0? [1-9]) | ((1|2) [0-9]) |30|31) $ "the correct format is;"01"~"09"and the"1"~"31". Regular expressions that match Chinese characters: [\u4e00-\u9fa5] matches double-byte characters (including kanji): [^\x00-\xff] Apply: Calculates the length of a string (a double-byte character length meter2, ASCII word counted1) String.prototype.len=function(){return  This. replace (/[^\x00-\xff]/G,"AA"). Length;} Regular expression matching blank line: \n[\s| ]*\ r matches the regular expression of the HTML tag:< (. *) > (. *) <\/(. *) >|< (. *) \/>Regular expression matching the leading and trailing spaces: (^\s*) | (\s*$) Application: There is no trim function like VBScript in JavaScript, and we can use this expression to do the following: String.prototype.trim=function() { return  This. Replace (/(^\s*) | ( \s*$)/g, ""); Use regular expressions to decompose and transform IP addresses: The following is a JavaScript program that uses regular expressions to match IP addresses and convert IP addresses to corresponding values:functionIP2V (IP) {re=/(\d+) \. (\d+) \. (\d+) \. (\d+)/g//regular expressions that match IP addressesif(Re.test (IP)) {returnRegexp.$1*math.pow (255,3)) +regexp.$2*math.pow (255,2)) +regexp.$3*255+regexp.$4*1 } Else { Throw NewError ("Not a valid IP address!"}} However, if the above program does not use regular expressions, it may be simpler to decompose directly with the split function, as follows:varip= "10.100.20.168"IP=ip.split (".") Alert ("the IP values are:"+ (ip[0]*255*255*255+ip[1]*255*255+ip[2]*255+ip[3]*1) matches the regular expression of an 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 Web Forms: use regular expressions to restrict input only in Chinese: onkeyup="Value=value.replace (/[^\u4e00-\u9fa5]/g, ")"Onbeforepaste="clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^\u4e00-\u9fa5]/g, ")"restrict input of full-width characters with regular expressions: onkeyup="Value=value.replace (/[^\uff0 0-\uffff]/g, ")"Onbeforepaste="clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^\uff00-\uffff]/g, ")"limit only numbers with regular expressions: onkeyup="Value=value.replace (/[^\d]/g, ")"Onbeforepaste="clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^\d]/g, ")"use Regular expression restrictions to enter only numbers and English: onkeyup="Value=value.replace (/[\w]/g, ")"Onbeforepaste="clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^\d]/g, ")"Regular expressions that match Chinese characters: [\u4e00-\u9fa5] Commentary: Matching Chinese is really a headache, with this expression is good to match double-byte characters (including Chinese characters): [^\x00-\xff] Commentary: Can be used to calculate the length of a string (a double-byte character length meter2, ASCII word counted1regular expression that matches a blank line: \n\s*\ R Commentary: a regular expression that can be used to delete blank lines matching HTML tags:< (\s*?) [^>]*>.*?| <.*? />Commentary: The online version is too bad, the above can only match the part, for complex nested tags still do not have the ability to match the white space characters of the regular expression:^\s*|\s*$ Commentary: can be used to delete whitespace characters at the end of a line (including spaces, tabs, page breaks, and so on), useful expressions that match the regular expression of an email address: \w+([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+)*Commentary: a regular expression that matches URL URLs is useful when forms are validated: [A-za-z]+://[^\s]*Commentary: The online version of the feature is limited, the above basic can meet the needs
JS Common Regular

Common regular expressions that need to be taken away

JS Common Regular

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.