Common JavaScript Regular matching rules code collection, very practical

Source: Internet
Author: User

collect some common JavaScript regular expression matching rules, such as matching phone number, Email, Chinese characters, social Security number, postal code, QQ number, filter blank line, match specific numbers, etc. Think this thing is very useful, but their own level of food, always do not write their own, read this document, feel a lot of practical, use the regular match, look at this, I believe to let you save a lot of time. Note: This is not limited to the use of JS, other languages can also be used for reference. Match a specific number: ^[1-9]\d*$//Match positive integer ^-[1-9]\d*$//Match negative integer ^-? [1-9]\d*$//Match integer ^[1-9]\d*|0$//Match non-negative integer (positive integer + 0) ^-[1-9]\d*|0$//Match non-positive integer (negative integer + 0) ^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$//Match positive floating point Number ^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*) $//Match negative floating point ^-? ([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0) $//Match floating point number ^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$//match nonnegative floating point number (positive floating point + 0) ^ (-([ 1-9]\d*\.\d*|0\.\d*[1-9]\d*)) |0?\.0+|0$//matches the non-positive floating point number (negative floating point + 0) matches Chinese characters: [\u4e00-\u9fa5] matches double-byte characters (including kanji): [^\x00-\xff], can be used to calculate the length of a string (a double-byte character-length meter 2,ascii character 1) matches a blank line: \n\s*\r, for example, to delete a blank line matching an HTML tag:<(\s*?) [^>]*>.*?</\1>|<.*? />Match the whitespace characters: ^\s*|\s*$, can be used to delete the line at the beginning of the end of the white space characters (including spaces, tabs, page breaks and so on), personal feel useful to match the email address: \w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) * Match URL url:[a-za-z]+://[^\s]* match user name is legal (letter beginning, allow 5-16 bytes, allow alphanumeric underline): ^[a-za-z][a-za-z0-9_]{4,15}$ match phone number: \d{3}-\d{ 8}|\d{4}-\d{7}, match form such as 0511-4405222 or 021-87888822 match QQ number: [1-9][0-9]{4,} match zip code: [1-9]\d{5} (?! \d), that is, match 6-digit matching ID: \d{15}|\d{18}, id 15-bit or 18-bit matching IP address: \d+\.\d+\.\d+\.\d+, extracting IP is useful. Match a specific string: ^[a-za-z]+$//matches a string consisting of 26 English letters ^[a-z]+$//matches a string consisting of 26 letters in uppercase ^[a-z]+$//matches a string consisting of 26 letters in lowercase ^[a-za-z0-9]+$/ /Match A string consisting of a number and 26 English letters ^\w+$//matches a string consisting of a number, 26 letters, or underscores 1900-2,099 date/^ ((19|20) [0-9]{2})-(([1-9]{1}) |10|11|12) $//^ \d{4}\-([1-9]|1[012]) $/Verification 12 months of the year: "^ (0?[ 1-9]|1[0-2]) $ "correct format:" 01 "-" 09 "and" 1 "" 12 "validate one months of 31 days:" ^ (0?[ 1-9]) | ((1|2) [0-9]) |30|31) $ "The correct format is:" 01 "" 09 "and" 1 "" 31 ". Only numbers can be entered: "^[0-9]*$" can enter only n digits: "^\d{n}$" can only enter at least n digits: "^\d{n,}$" can only enter numbers for m-n digits: "^\d{m,n}$" can only enter numbers that begin with 0 and not 0: "^ (0|[ 1-9][0-9]*) $ "can only enter positive real numbers with two decimal places:" ^[0-9]+ (. [ 0-9]{2})? $ "can only enter a positive real number with 1-3 decimal places:" ^[0-9]+ (. [ 0-9]{1,3})? $ "can only enter a non-zero positive integer:" ^\+? [1-9] [0-9]*$] can only enter a non-zero negative integer: "^\-[1-9][0-9]*$ "can only enter characters of length 3:" ^. {3}$ "can only enter a string consisting of 26 English letters:" ^[a-za-z]+$ "can only enter a string consisting of 26 uppercase English letters:" ^[a-z]+$ "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]+$" can only enter a string consisting of a number, 26 letters, or an underscore: "^\w+$" validates the user password: "^[a-za-z]\w{5,17}$" is in the correct format: start with a letter , the length is between 6-18 and can only contain characters, numbers, and underscores. Verify that it contains ^%& ',; =?$\ "characters:" [^%& ", =?$\x22]+" can only input Chinese characters: "^[\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 date format for 2005-1-1 cannot be 2005-01-01, see an example:
<script language=javascript>var str= "2005-04-03"; re=/\d{4} (-(?! 0)) \d{1,2} (-(?! 0)) \d{1,2}/if(!  Re.test (str)) {alert ("Malformed")}else{alert ("correct")}</script>

Common JavaScript Regular matching rules code collection, very practical

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.