Regular expressions, also known as regular representations and regular representations. (English: Regular Expression, often abbreviated as regex, regexp or RE in code), a concept of computer science. Regular expressions use a single string to describe and match a series of syntactic rules. In many text editors, regular expressions are usually used to retrieve and replace text that conforms to a certain pattern.
1. Classification
1. "^" Similar
"^" matches the start position of the input string.
"$" matches the end position of the input string.
"\b" matches a word boundary, which means the position between the word and space. (For example: "er\b" can match "er" in "never", but cannot match "er" in "verb")
"\B" and "\b" instead match non-word boundaries (for example: "er\b" can match "er" in "verb", but cannot match "er" in "never")
2. "*" Similar
Asterisk (*): An asterisk means matching any pass (0 or any time) of the character before it.
Plus sign (+): means matching the previous character once or more times (at least once).
Question mark (?): A question mark is also a quantity word, which represents 0 or 1 time matching the previous character.
Brackets[]: Brackets are used to represent a character set. If this set has many elements, such as 26 letters, numbers, etc., written in brackets one by one, it would be too troublesome, so we generally use hyphen to represent a range (for example: [a-z] represents a set of lowercase letters; [a-zA-Z] represents a set of uppercase letters). The off-character "^" means matching any characters that are not in the set. (Example: [^a-z]).
Braces {}: The purpose of braces refers to how many times the previous character is repeated (for example: {N}: repeat n times; {n,m}: repeat n~m times; {n,}: repeat n times at least n times; {,m}: repeat at most m times)
3. "\s" Similar
"\w" (lowercase w) means letters or numbers, equivalent to [a-zA-Z0-9]
"\W" (caps W) means non-letter and non-number, contrary to \w, it is equivalent to [^a-zA-Z0-9]
"\s" (lowercase s) means matching a null character, including space, line break, carriage return, tab, equivalent to [ \n\r\t\f]
"\S" (capsulated S) matches non-space characters, contrary to \s, equivalent to [^ \n\r\t\f]
"\d" (lowercase d) represents a decimal number, equivalent to [0-9]
"\D" (caps D) matches a non-numeric character, equivalent to [^0-9]
2. Declare a checkNum function
function checkNum(input){ /* means that it must be 1 or more numbers*/ var regex = /^[A-Z]{6,12}$/; if ((regex)) { return true; } else { return false; } }
3. Declare a checkEmail function
function checkEmail(input){ var regex = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/; if ((regex)) { return true; } else { return false; } } }
4. Encapsulate a trim()
var my = function() {}; = { ltrim: function(str) { return (/(^\s*)/g,''); }, rtrim: function(str){ return (/(\s*$)/g,''); }, trim: function(str){ return (/(^\s*)|(\s*$)/g,''); } } /*^ Start with xx*/ /*\s means space*/ /** means matching zero or more*/ /*g means matching all*/ /*(^\s*) means matching one or more characters starting with a space*/ /*(/(^\s*)/g,'') means to replace all spaces with ''*/
Attached:A complete collection of commonly used regular expressions! (For example: match Chinese, match html)
Regular expression matching Chinese characters: [u4e00-u9fa5]
Comment: Matching Chinese is really a headache, it's easy to do with this expression
Match double-byte characters (including Chinese characters): [^x00-xff]
Comment: It can be used to calculate the length of a string (a double-byte character length meter 2, ASCII character meter 1)
Regular expression matching blank lines: ns*r
Comment: Can be used to delete blank lines
Regular expression matching HTML tags: <(S*?)[^>]*>.*?|<.*? />
Comment: The version circulating online is too bad, and the above one can only match the part, and it is still powerless to use complex nested markers.
Regular expression matching the beginning and end whitespace characters: ^s*|s*$
Comment: It can be used to delete whitespace characters at the beginning and end of the line (including spaces, tabs, page breaks, etc.), a very useful expression
Regular expression matching the email address: w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*
Comment: It is very practical when verifying the form
Regular expression matching URL: [a-zA-z]+://[^s]*
Comment: The functions of the version circulating online are very limited, and the above can basically meet the needs
Match whether the account is legal (beginning with letters, 5-16 bytes allowed, alphanumeric underscores allowed): ^[a-zA-Z][a-zA-Z0-9_]{4,15}$
Comment: It is very practical when verifying the form
Match domestic phone number: d{3}-d{8}|d{4}-d{7}
Comment: Matching forms are as follows: 0511-4405222 or 021-87888822
Match Tencent QQ number: [1-9][0-9]{4,}
Comment: Tencent QQ number starts at 10,000
Match the Chinese postal code: [1-9]d{5}(?!d)
Comment: China's postal code is 6 digits
Match ID card: d{15}|d{18}
Comment: China's ID card is 15 or 18 digits
Match ip address: d+.d+.d+.d+
Comment: It is useful when extracting IP addresses
Match specific numbers:
^[1-9]d*$ //Match positive integer
^-[1-9]d*$ //Match negative integers
^-?[1-9]d*$ //Match integers
^[1-9]d*|0$ //Match non-negative integers (positive integer + 0)
^-[1-9]d*|0$ //Match non-positive integers (negative integer + 0)
^[1-9]d*.d*|*[1-9]d*$ //Match positive floating point number
^-([1-9]d*.d*|*[1-9]d*)$ //Match negative floating point numbers
^-?([1-9]d*.d*|*[1-9]d*|0?.0+|0)$ //Match floating point numbers
^[1-9]d*.d*|*[1-9]d*|0?.0+|0$ //Match non-negative floating point numbers (positive floating point numbers + 0)
^(-([1-9]d*.d*|*[1-9]d*))|0?.0+|0$///Match non-positive floating point numbers (negative floating point numbers + 0)
Comment: It is useful when processing large amounts of data, please pay attention to corrections when applying it in detail.
Match a specific string:
^[A-Za-z]+$//Match a string composed of 26 English letters
^[A-Z]+$//Match a string composed of 26 English letters capitalizations
^[a-z]+$//Match a string composed of 26 English letters lowercase
^[A-Za-z0-9]+$//Match a string composed of numbers and 26 English letters
^w+$//Match a string composed of numbers, 26 English letters or underscores
Only enter numbers: "^[0-9]*$"
Only n-digit numbers can be entered: "^d{n}$"
Only at least n digits can be entered: "^d{n,}$"
Only the number of m-n bits is entered: "^d{m,n}$"
Only numbers starting with zero and non-zero are entered: "^(0|[1-9][0-9]*)$"
Only positive real numbers with two decimal places can be entered: "^[0-9]+(.[0-9]{2})?$"
Only positive real numbers with 1-3 decimal places can be entered: "^[0-9]+(.[0-9]{1,3})?$"
Only non-zero positive integers can be entered: "^+?[1-9][0-9]*$"
Only non-zero negative integers can be entered: "^-[1-9][0-9]*$"
Only characters with length 3 can be entered: "^.{3}$"
Only strings composed of 26 English letters can be entered: "^[A-Za-z]+$"
Only strings composed of 26 capital English letters can be entered: "^[A-Z]+$"
Only strings composed of 26 lowercase English letters can be entered: "^[a-z]+$"
Only strings composed of numbers and 26 English letters can be entered: "^[A-Za-z0-9]+$"
Only strings composed of numbers, 26 English letters or underscores can be entered: "^w+$"
Verify user password: "^[a-zA-Z]w{5,17}$" is the correct format: start with a letter, with a length between 6-18.
Only characters, numbers, and underscores are included.
Verify that it contains characters such as ^%&'',;=?$": "[^%&'',;=?$x22]+"
Only enter Chinese characters: "^[u4e00-u9fa5],{0,}$"
Verify the 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: "XXXX-XXXXXXX", "XXXX-XXXXXXXXX", "XXX-XXXXXXXXX",
“XXX-XXXXXXXX”,“XXXXXXX”,“XXXXXXXX”。
Verify ID number (15 or 18 digits): "^d{15}|d{}18$"
Verify the 12 months of one year: "^(0?[1-9]|1[0-2])$" is correct in the format: "01"-"09" and "1" and "12"
31 days of verification for one month: "^((0?[1-9])|((1|2)[0-9])|30|31)$"
The correct formats are: "01", "09", and "1", "31".
Regular expression matching Chinese characters: [u4e00-u9fa5]
Match double-byte characters (including Chinese characters): [^x00-xff]
Regular expression matching blank lines: n[s| ]*r
Regular expression matching HTML tags: /<(.*)>.*|<(.*) />/
Regular expression matching the beginning and end spaces: (^s*)|(s*$)
Regular expression matching the email address: w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*
Regular expression matching URL: http://([w-]+.)+[w-]+(/[w- ./?%&=]*)?*/
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.