Master advanced matching operation
Regular expressions are really fun. In the last article, you should master some basic operations. Presumably, you should master them well. Then this article will take you to master some advanced operations.
Introducing metacharacters
Regular expression language consists of two basic character types: literal (normal) text characters and metacharacters.
Metacharacters enable regular expressions to be processed.
The so-called metacharacters refer to those special characters with special meaning in regular expressions, which can be used to specify the occurrence mode of their leading characters (i.e. characters in front of metacharacters) in the target object.
The above introduction to metacharacters comes from the network and is for reference only.
The metacharacters introduced this time are [] (and). What is the role?
In the last article, we learned that we can match any character through English period. But sometimes we can clearly match from some characters. For example, we just want to match C and H.
In regular expressions, we can define a character set through the metacharacter []. This will match the members in the collection.
Let's look at the example directly:
let str = 'Chocolate wants 100,000 fans, Chocolate love chocolate'; let reg = /[Cc]hocolate/g; let res = str.match(reg); console.log(res); // ['Chocolate', 'Chocolate', 'chocolate']
From the results, we can find that the match to chocolate, the first letter can be case sensitive.
Next, our example may become an array, as follows:
let arr = ['Choco','choco', 'nhoco', 'yhoco', 'hearling']; let reg = /[Ccyn]hoco/g; let res = arr.filter(item=>item.match(reg)); console.log(res); // ['Choco', 'choco', 'nhoco', 'yhoco']
The above examples are also relatively clear. You must have mastered the concept of metacharacters and.
With this knowledge, you can match one of the known multiple characters, and you can also narrow the problem of global matching letter case.
Match number
In the previous content, it seems that we have always matched the letters in the string without matching numbers. Now let's use metacharacters to solve a series of problems of matching numbers.
Let's look at the example directly:
let arr = ['choco1','choco2', 'nhoco3', 'yhoco6', 'hearling']; let reg = /[Ccyn]hoco[0123456789]/g; let res = arr.filter(item=>item.match(reg)); console.log(res); // ['choco1', 'choco2', 'nhoco3', 'yhoco6']
We have defined the set of numeric classes through [], which is easy to understand and will not be explained more.
But will it be a little troublesome to write this way? You can omit some writing.
Of course, the following examples can be given directly:
let arr = ['choco1','choco2', 'nhoco3', 'yhoco6', 'hearling']; let reg = /[Ccyn]hoco[0-9]/g; let res = arr.filter(item=>item.match(reg)); console.log(res); // ['choco1', 'choco2', 'nhoco3', 'yhoco6']
Set interval
We can omit the number set through 0-9 in the set, which is called using the character set interval.
Of course, there are number sets and letter sets, as follows:
A-z, A-z and other set intervals, but note that if you want to include the case of letters, it is not possible to use A-z. you can check the ASCII character table. Z to a are not continuous and there are other characters, so this is generally not possible.
With these combinations, you must be able to write a lot of advanced matches. I won't give examples here. You can master this knowledge.
Out of interval
Send you away, thousands of miles away
In the last section, we mastered the use of interval, but this is the interval we know. What about those outside the interval?
Or sometimes we know that we want to match everything except letters, or we want to match everything except numbers. What should we do?
If you are a big fan of the law of violence, you may enumerate all the situations, but can you consider it completely sometimes? Obviously not.
In fact, it's very simple. Don't you just take an opposite? Look directly at the following examples:
let arr = ['choco1','chocol', 'nhoco3', 'yhoco6', 'hearling']; let reg = /[Ccyn]hoco[^0-9]/g; let res = arr.filter(item=>item.match(reg)); console.log(res); // ['chocol']
Look at the example, take the inverse character as ^. In this example, the last character is matched with a non number. It is much more convenient through the inversion operation. There is no need to consider many situations.
Summary
Wuhu, this section is over. Thank you for reading. We look forward to the next one.