(This is too difficult to learn! (+) Answer (--) Well, although the previous content is quite simple, the basic knowledge is actually about the architecture. Of course, it cannot be used only for discussion on paper, it's really Zhuge ~! Okay, let's start our advanced journey (I... SyntaxHighlighter. all ();
(This is too difficult to learn! (+ =) Translate (-) Well, although the previous content is quite simple, the basic knowledge is actually about the same architecture. Of course, it cannot just be used in paper, it's true that we need to use it more ~!
Okay. Next we will start our advanced journey (I admit that I am actually a Trojan horse +. +)
First of all, what is a regular expression? In fact, the introduction of the concept often does not help us understand what it is, so I will briefly describe it, it is actually a string that records string rules. After reading this part, we can understand what it is.
Basic Syntax: the regular expression is the "/expression/" + symbol indicating the search range. For example, the keyword "function" is/function/gi, where g indicates global, global search, I indicates ignor, and case-insensitive.
In js, we use the RegExp class.
There are many symbols in this class to indicate different indexing methods. First, let's put a column in this table:
Metacharacters |
Description |
Quantifiers |
Description |
Negative characters |
Description |
. |
Match any character except the newline character (\ n) |
* |
Number of occurrences: [0, + ∞) |
\ W |
Letters, numbers, underscores (_), other than Chinese Characters |
^ |
Start of matching string |
+ |
Number of occurrences: [1, + ∞) |
\ S |
Characters other than white space characters |
$ |
End of matching string |
? |
Number of occurrences: [0, 1] |
\ D |
Characters other than numbers |
\ B |
Match word boundary |
{N} |
Number of occurrences: n |
\ B |
Match non-word boundary |
\ D |
Matching number |
{N ,} |
Number of occurrences: [n, + ∞) |
[^] |
Any character other than the character string after the ^ character in the character class |
\ S |
Match any blank space character |
{N, m} |
Number of occurrences: [n, m] |
[-] |
Match the characters/numbers in the string from-prefix to-suffix |
\ W |
Match letters, numbers, underscores or Chinese Characters |
|
|
|
|
In addition to the above symbols, there are three concepts: group, reverse reference, and candidate.
Grouping: Enclose strings with (), so that strings can be combined according to certain rules. At the same time, parentheses can also be nested. For example, the regular expression is used to express the Date Format: var dateReg =/^ (\ d })(-) (\ d {1, 2} (-) (\ d {1, 2}) $). Of course, this method also has some vulnerabilities. Here, it only indicates the format problem.
In addition to square brackets, for example, if you only want to match one of the letters x y z d w, write [xyzdw]. If the matching is continuous, for example, a 0-4 number can be [0-4], but it only represents one character.
If you want to write multiple, such as matching the ac or bd, use the "|" symbol to write (ac | bd ).
For example, if we want to match a string that only contains abc, we can write: abdReg =/^ [abc] + $/; below is a complete example.
Regular express