Regular expression

  • Last update:  2021-03-30
  • I. Description

    Sometimes we need to use regular expressions, for example, regular expressions are used in the text widget filling rules, as shown in the following figure:

    1.png

    The rules for writing regular expressions are as follows:

    For example, enter the regular expression to verify the user password: "^[a-zA-Z]\w{5,17}$" means that the password rule starts with a letter, the length is between 6-18, and can only contain letters and numbers And underscore.

    • l \d Any number, any one of 0~9

    • l \w Any letter or number or underscore, that is, any one of A-Z, a-z, 0-9

    • l \s includes any one of blank characters such as spaces, tabs, and form feeds

    • l . The decimal point can match any character except the newline character (\n)

    • l ^ start

    • l $ end

    The role of some expressions:

    • {n}: Represents repeated n times, for example, "\w{2}" is equivalent to "\w\w"; "a{5}" is equivalent to "aaaaa"

    • {m,n}: means repeat at least m times and at most n times. For example, "ba{1,3}" can match "ba" or "baa" or "baaa"

    • {m,}: means repeat at least m times, for example, "\w\d{2,}" can match "a12","_456","M12344"

    • ?: Match expression 0 or 1 time, equivalent to {0,1}, for example, "a[cd]?" can match "a","ac","ad"

    • +: The expression appears at least once, equivalent to {1,}, for example, "a+b" can match "ab","aab","aaab"

    • *: The expression does not appear or appears any number of times, equivalent to {0,}, for example, "\^*b" can match "b", "^^^b".

    Some examples of common small examples:

    (1) Check whether the mobile phone number is legal: ^1(3\d|5[36789])\d{8}$

    Explanation: The mobile phone number has 11 digits, the first digit is 1, the second digit is 3 or 5, when the second digit is 5, the third digit can only be one of 36789

    (2) Check whether the Chinese name is legal: ^[\u4e00-\u9fa5]+$

    Explanation: If it is not Chinese or there are spaces before and after Chinese, it is considered illegal

    (3) Check whether the HTML tags match: <(.*)>.*<\/\1>|<(.*) \/>

    Explanation: If only <html> and no corresponding</html> appear, the test result is that the HTML tags do not match

    (4) Check whether the URL format is legal: [a-zA-z]+://[^\s]*

    Explanation: The legal format of the URL should start with the letter link ://

    (5) Check whether the IP format is legal: (\d+)\.(\d+)\.(\d+)\.(\d+)

    Explanation: The format of the IP is in dotted decimal notation, for example: 192.168.100.11 is the correct IP

    (6) Check whether the account is legal: ^[a-zA-Z][a-zA-Z0-9_]{4,15}$

    Explanation: The account number starts with a letter, the other digits can have uppercase and lowercase letters, numbers, and underscores, allowing 5-16 bytes

    (7) Check whether the QQ number is legal: ^\s*[0-9]{4,9}\s*$

    Explanation: QQ number must be a number, the shortest is 5 digits, and the longest is 10 digits

    (8) Check whether the postal code is legal: ^[1-9]\d{5}$

    Explanation: The first digit of the postal code is not 0, and the length is 6 digits

    (9) Check whether the ID number is legal: (^\d{14}$)|(^\d{17}([0-9]|X)$)

    Explanation: ID card number is 15 or 18 digits, 15 digits are all digits, the first 17 digits of 18 digits are digits, and the last digit is check digit, which may be digits or characters X

    (10) Check whether the time is compliant: ^(0\d{1}|1\d{1}|2[0-3]):([0-5]\d{1})$

    Explanation: The time is in a 24-hour system, and the number before the number is 01 to 23

    (11) Check whether the password is compliant: ^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$

    Explanation: The password must contain a combination of uppercase and lowercase letters and numbers, special characters cannot be used, and the length is between 8-10

    (12) Check whether the mailbox is compliant: Because there are many types of mailbox formats, the most commonly used is ^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a- zA-Z0-9_-]+)+$

    Explanation: Only English letters, numbers, underscores, English periods, and underscores are allowed

    If the name contains Chinese characters, letters, and numbers, only English domain names are allowed in the domain name. The regular expression is ^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\ .[a-zA-Z0-9_-]+)+$

    (13) Check whether there are spaces in the input content: ^[^\s]*$

    Explanation: Use when the input content is not allowed to contain spaces, the regular expression is ^[^\s]*$

    Note: If you are using regular expressions in j JS, you need to add "/" before and after the regular expressions, such as checking whether the mobile phone number is legal: /^1(3\d|5[36789])\d{8} $/

    Attachment List


    Theme: Report Features
    • Helpful
    • Not helpful
    • Only read

    滑鼠選中內容,快速回饋問題

    滑鼠選中存在疑惑的內容,即可快速回饋問題,我們將會跟進處理。

    不再提示

    10s後關閉

    Get
    Help
    Online Support
    Professional technical support is provided to quickly help you solve problems.
    Online support is available from 9:00-12:00 and 13:30-17:30 on weekdays.
    Page Feedback
    You can provide suggestions and feedback for the current web page.
    Pre-Sales Consultation
    Business Consultation
    Business: international@fanruan.com
    Support: support@fanruan.com
    Page Feedback
    *Problem Type
    Cannot be empty
    Problem Description
    0/1000
    Cannot be empty

    Submitted successfully

    Network busy