Regular expressions are used for string processing, form validation, and other occasions, and are practical and efficient. Here are some commonly used expressions collected here for future reference.
Regular expression for matching Chinese characters: [\u4e00-\u9fa5]
Note: Matching Chinese characters can be a headache, but with this expression, it becomes easier.
Regular expression for matching double-byte characters (including Chinese characters): [^\x00-\xff]
Note: It can be used to calculate the length of a string (a double-byte character counts as 2, ASCII characters count as 1).
Regular expression for matching blank lines: \n\s*\r
Note: It can be used to delete blank lines.
Regular expression for matching HTML tags: <(\S*?)[^>]>.?</\1>|<.*? />
Note: The versions circulating online are terrible, and this one can only match part of it, still powerless for complex nested tags.
Regular expression for matching leading and trailing whitespace characters: ^\s*|\s*$
Note: It can be used to delete leading and trailing whitespace characters (including spaces, tabs, page breaks, etc.), a very useful expression.
Regular expression for matching email addresses: \w+([-+.]\w+)@\w+([-.]\w+).\w+([-.]\w+)*
Note: Very useful for form validation.
Regular expression for matching URLs: [a-zA-z]+://[^\s]*
Note: The versions circulating online have limited functionality, and this one can basically meet the requirements.
Regular expression for matching valid accounts (starting with a letter, allowing 5-16 bytes, allowing alphanumeric and underscore characters): ^[a-zA-Z][a-zA-Z0-9_]{4,15}$
Note: Very useful for form validation.
Regular expression for matching domestic phone numbers: \d{3}-\d{8}|\d{4}-\d{7}
Note: Matches the format like 0511-4405222 or 021-87888822.
Regular expression for matching Tencent QQ numbers: [1-9][0-9]{4,}
Note: Tencent QQ numbers start from 10000.
Regular expression for matching Chinese postal codes: [1-9]\d{5}(?!\d)
Note: Chinese postal codes are 6-digit numbers.
Regular expression for matching ID cards: \d{15}|\d{18}
Note: Chinese ID cards are 15 or 18 digits.
Regular expression for matching IP addresses: \d+.\d+.\d+.\d+
Note: Useful for extracting IP addresses.
Regular expression for matching specific numbers:
^[1-9]\d*$ // Matches positive integers
^-[1-9]\d*$ // Matches negative integers
^-?[1-9]\d*$ // Matches integers
^[1-9]\d*|0$ // Matches non-negative integers (positive integers + 0)
^-[1-9]\d*|0$ // Matches non-positive integers (negative integers + 0)
^[1-9]\d*.\d*|0.\d*[1-9]\d*$ // Matches positive floating-point numbers
^-([1-9]\d*.\d*|0.\d*[1-9]\d*)$ // Matches negative floating-point numbers
^-?([1-9]\d*.\d*|0.\d*[1-9]\d*|0?.0+|0)$ // Matches floating-point numbers
^[1-9]\d*.\d*|0.\d*[1-9]\d*|0?.0+|0$ // Matches non-negative floating-point numbers (positive floating-point numbers + 0)
^(-([1-9]\d*.\d*|0.\d*[1-9]\d*))|0?.0+|0$ // Matches non-positive floating-point numbers (negative floating-point numbers + 0)
Note: Useful for handling large amounts of data, pay attention to adjustments when applying.
Regular expression for matching specific strings:
^[A-Za-z]+$ // Matches strings consisting of 26 English letters
^[A-Z]+$ // Matches strings consisting of 26 uppercase English letters
^[a-z]+$ // Matches strings consisting of 26 lowercase English letters
^[A-Za-z0-9]+$ // Matches strings consisting of numbers and 26 English letters
^\w+$ // Matches strings consisting of numbers, 26 English letters, or underscores.