I used to love and hate regular expressions. What I love is that there are many regular expressions written by others on the Internet that can be directly used to simplify the work code. What I hate is that once the requirements of regular matching are adjusted, I cannot understand the regular expressions written by others and do not know how to modify them. This column will study these days their own re learning experience to share with you, I hope to help you.

1. Look at the regular benefits first

Requirement: Remove whitespace from ‘ab C D e’ string

Not using the re:

// Convert string -> to array -> let STR = 'ab c d e'; let strToArr = str.split(' '); let ArrToStr = strToArr.join('');Copy the code

Use the re:

// let STR = 'ab c d e'; let res = str.replace(/\s/g, '');Copy the code

From the data processing above we can see the advantages of regular:

  1. Convenient, only need one line of code can solve the problem;
  2. Efficient, processing on the original data type without changing the data type;

But also regular has its own disadvantages:

May look not to understand for beginners, not friendly and \ s g represents what at first glance very troublesome, but as the grasp of the regular after more and more skilled this shortcoming also ceased to exist, as long as you understand the rules of the it can with very little code to write js in complex matching rules, to learn the basic rules of regular below!

2. Rerule the ground rules

1. Two forms

One is instantiated form and one is literal form. We use the literal form to declare a re, and the instantiation form is usually used only when the match is a variable.

Let reg = new RegExp('test', 'g'); let str = 'this is test'; reg.test(str); // let reg = /test/g let STR = 'this is test' str.test(reg)Copy the code

2. Use /test/g as an example to illustrate the regular meaning

This expression can be understood in two paragraphs:

  1. /test/ The test in the two slashes indicates whether the match contains the test string
  2. The g after /test/ stands for the range of the match

Matches a string globally to see if it contains at least one test string

Let reg = /test/g // 1. String does not contain test 'tes'. Match (reg) -> null // 2. Match (reg) -> ['test'] // 3. 'test is a test'. Match (reg) -> ['test', 'test']Copy the code

We find that multiple tests will be matched. At this time, it is because G operates the matching range for global matching. If we remove G, only the previous tests will be matched

let reg = /test/; 'test is a test'. Match (reg) -> ['test'] ('test', index: 0, input: 'test is a test', groups: Select * from 'undefined' where 'test' = 'test';Copy the code

It follows that the letter at the end of the re represents the matching condition. There are other matching conditions besides g for global matching such as I ignoring case and m matching multiple lines

Let reg = / test /g; console.log('test is a test'.match(reg)); Let reg = /TEST/gi; console.log('test is a test'.match(reg)); // ['test', Let STR = 'test is a test.\nTest is here' let STR = 'test is a test.\nTest is here' let reg = /^ test /g -> Let reg = /^Test/gm -> let reg = /^Test/gm -Copy the code

3. []

In the above example, we can see that the key to a successful /test/ match is that the string must have a full ‘test’. If a t (‘tes’) is missing, the match will not succeed. If we want to match a bit by bit, then we need to use the [] expression. For example, if I have a requirement to match a string that contains either ‘ABC’ or ‘BBC’, I can write:

Let reg = /[ab] BC /gCopy the code

When you encounter [], don’t panic. It can be simply understood that there are several brackets to match several bits, for example:

Let reg = /[wx][xy][z]/gCopy the code

4. Rematches the default matching rules

There are two important points in re

  1. Default greedy mode, can match many match not few
  2. If it’s matched, it’s not going to be matched again

Here’s an example:

Let reg = /a+/g; let reg = /a+/g; let str = 'this a dog or aa god or aaa good'; str.match(reg); -> ['a', 'aa', 'aaa']Copy the code

[a, a, a, a, a, a, a] A brief description of the matching rules:

  1. First, match from left to right, match the first A, find only one at the same time meet the requirements and record this A;
  2. After the first a continues to match, it is found that there are two consecutive matching conditions of regular aa after OR, and the value of AA is also recorded if the default is greedy mode.
  3. Aaa also matches aa.
  4. Until the match ends, the result is [‘a’, ‘AA ‘,’ AAA ‘]

5. At the end

This article mainly explains the basic usage and basic rules of regular. In fact, there are many knowledge points, such as how to use regular to match complex rules, implement template string replacement, the use of () expressions, and the replace method of heavy and difficult string and regular expression collocation, etc. Of course, you need to know all the basics of re, so next time, let’s share quantifiers and metacharacters in re.