This article talks about the basics of regular expressions of JavaScript. If you are not familiar with JavaScript regular expressions, you can just come and learn them. It talks about the basics of JavaScript. Regular expressions, for those who are not familiar with them, let’s take a look!
Today I learned the RegExp object when I was learning javascript , take the opportunity to learn regular expressions. I have never been exposed to them before, so I take the opportunity to learn them. It is very comfortable.
Reference website: 30-minute introductory tutorial on regular expressionsClick to open the link
Regular expressions are used to express string matching rules.
Metacharacters are defined in regular expressions A special symbol added to regular expressions to replace certain rules.
\b | represents the beginning or end of a word |
. | represents except line breaks Any character other than |
* | represents any number of the character that appears before *, such as a*, which represents any number of a before it (repeat 0 times or multiple times) |
means + any number of the characters that appear before , such as a+, means there is any number of a in front (repeated one or more times) |
Repeat 0 or 1 times | Repeat greater than or equal to n times |
{n, m} | Repeat n to m times |
represents a number from 0 to 9 |
Matches letters or numbers or underscores or Chinese characters tab character, Newline characters, Chinese full-width spaces, etc. |
##^ | |
# & | ## The end of the matching string
Matches any characters that are not letters, numbers, underscores, or Chinese characters | |
Matches any character that is not a digit | |
Matches any character that is not the beginning or end of a word The characters at position | |
match any character that is not whitespace | |
Match characters other than x | |
Match characters other than aeiou |
For example, the regular expression ^\S+& is used to match a string of characters that does not contain whitespace charactersString
The content of this part matches the previous grouping. When we use () to group characters, we can use this grouping by numbering The method will be quoted later. For grouping by (), grouping starts from 1 in the order of ( (). For example, the regular expression \b(\w+)\s+\1\b can be used to match repeated words. , such as go go, etc. Here, the group that appeared before is referenced through \1
## Other involved back reference syntax is:
Match exp and capture the current content into automatic groupings name>exp) | Match exp, and capture the current content and assign the group name name |
(?:exp) | Match exp, do not assign the captured content group name |
10. Zero-width assertionis used to find the part before or after a certain part of content but does not include the content. Regular expression (?=exp) means to assert that the part that appears later can match the expression exp. For example, \b\w+(?=ing\b) matches the front part of words ending in ing. For example, when searching for I'm dancing and singing, dance and singing will be matched (because there is \w+, it will not be matched as s). Regular expression (?<=exp) means to assert that the previous part can match the expression exp. For example, (?<=\bre)\w+\b will match the second half of the word starting with re. For example, when searching for reading, ading will be matched. If you want to add a comma to every three digits of a very long number, such as adding a comma to 123456789, you can use the regular expression ((?<=\d)\d {3})+\b, the search result is 234567890 (I don’t understand this part of the search rules...) The following example uses two assertions at the same time (?=<\ s)\d+(?=\s), used to match numbers between two blank characters, excluding blank characters. Generally speaking, the purpose of zero-width assertion is to determine the starting point or ending point of matching characters according to certain rules. 11. Negative zero-width assertionAs mentioned earlier, use antonyms to find characters that are not a certain character or are not in a certain character. characters. For example, if you want to find a word where the letter q appears but is not followed by u. You might write \bq[^u]\w*\b. But for such an expression, an error will occur when q appears at the end of a word, because [^u] will match the separator character of the word, which will then match the next word, which will match characters such as Iraq fighting. string. In order to solve the antonym occupancy problem, we can use a negative zero-width assertion, because it only matches one position and does not consume any characters. The above expression can be written as \bq(?!u)\w*\b. ## Similarly, we use (? ## A more complex example: (?<=<(\w+)>).*(?=<\/\1> ) When you see the previous (?<=) and the following (?=), you know that both the front and back are zero-width assertions, and <( \w+)> represents the html tag. If the previous one is 12. Comments Include comments through the syntax (?#comment), For example, 2[0-4]\d(?#200-249). 13. Greed and laziness Similar to the flag in js, there are case insensitivity, multi-line mode, global mode, etc. This part is to deal with the matching problem, for example, if you want to match the mathematical expression (5*3))) (5*3) cannot simply be written as \(.*\), as this will match the entire expression. Then the matching strategy that should be adopted is similar to the bracket matching problem we have learned. Use the stack to solve it. When encountering (pressing the stack, encountering) popping the stack, if the last stack is empty, this means that the brackets in the expression completely match. If If not empty, the regex engine will backtrack to make the brackets match. Related recommendations: How to use regular expressions in JS |
The above is the detailed content of Learn the basics of regular expressions. For more information, please follow other related articles on the PHP Chinese website!