Regular expressions use a single string to describe and match a series of strings that match a syntactic rule.

Well, that’s it:

  • Regular expressions describe rules
  • Regular expressions operate on strings

This blog post is just a note I read…

For example: digit refers to the number [0-9], converted to the regular keyword \d, and its uppercase \d means not digit, i.e., not [0-9].

[^ ^ and the regex]

This is placed before the rule to indicate the beginning of the match. [^regex] is logical not.

console.log((/^I am/).test('I am a teacher.')); // true
Copy the code
console.log((/[^\d]/).test('aa')); // true
Copy the code

[/ b], and \ b

[\b] denotes backspace; While \b refers to the word border.

let str = 'teacher\b';
console.log(/[\b]/.test(str)); // true
Copy the code
let str = 'I am a teacher.'
console.log(str.replace(/\bam\b/.'am not')); // 'I am not a teacher'
Copy the code
let str = 'Mike is a teacher. And Mike love this job! '
let pattern = /\bmike\b/gi;
console.log(str.replace(pattern, 'Jay')); // 'Jay is a teacher. And Jay love this job! '
Copy the code

Set interval []

The interval of the set is in brackets [and]. The [\b] fallback symbol can be treated specially.

console.log(/[a-z]/.test('bb')); // true
console.log(/[a-z]/.test('BB')); // false
Copy the code

Group ()

All regular expressions contained in (and) metacharacters are grouped into groups, each of which is a subexpression.

There are capture types () and non-capture types (? :), forward looking (? =) and reverse forward-looking (? !). And so on.

  • Capture (pattern)

The match results are stored in a buffer for later use.

let dateStr = '2019/07/16';
let reg = /(\d{4})\/(\d{2})\/(\d{2})/;
dateStr = dateStr.replace(reg, '$1 - $2 - $3');
console.log(dateStr); / / "2019-07-16"
Copy the code
  • Non-capture type (? :pattern)

The pattern part is combined into a unified operable combination item, but it is not captured as a sub-match, and the matching part is numbered and not stored in the buffer for later use. The non-capturing grouping method is useful in situations where you have to combine, but you don’t want to cache the combined parts.

For example, in an article for the program and the project of two words, regular expressions can be expressed as/program | project /, can also be expressed as/pro (” gramm | ject) /, but cache matching (gramject) has no meaning, you can use/pro (? : “gramm | ject)/non capture matching that can both concise match don’t cache again without meaningful words match.

let dateStr = '2019/07/16';
let reg = / (? :\d{4})\/(\d{2})\/(\d{2})/;
dateStr = dateStr.replace(reg, '$1 - $2 - $3');
console.log(dateStr); / / "07-16 - $3"
console.log(RegExp. $3); / /"
Copy the code
  • Forward looking (? =pattern)

There must be a partial match of pattern in the corresponding position of the target string, but it is not treated as a match result, and it is not stored in the buffer for future use.

A good example: if you stand in place and look ahead, return true if the specified object is in front of you, false otherwise.

let str = 'I am a teacher';
let pattern = /I am a (? =teacher)/
console.log(pattern.test(str)); // true
console.log(RegExp. $1); / /"
Copy the code
  • Reverse foresight (? ! pattern)

A good example: if you stand in place and look ahead, return true if the specified object is not in front of you, false otherwise.

let str = 'I am a teacher';
let pattern = /I am a (? ! teacher)/
console.log(pattern.test(str)); // false
console.log(RegExp. $1); / /"
Copy the code

reference$1, $2...

As mentioned in the above grouping, adding parentheses around a regular expression pattern or part of a pattern will cause that part of the expression to be stored in a temporary buffer. Can you use non-capture metacharacters? : and? =, or? ! To ignore the saving of this regular expression.

Each child match captured is stored as what is encountered from left to right in the regular expression pattern. Buffers that store child matches are numbered starting at 1 and continuously numbered up to a maximum of 99 child expressions.

You can use $1, $2… Match access one by one, for example:

let dateStr = '2019/07/16';
let reg = /(\d{4})\/(\d{2})\/(\d{2})/;
reg.test(dateStr);
console.log(RegExp. $1); / / 2019
console.log(RegExp. $2); / / 07
console.log(RegExp. $3); / / 16
Copy the code

reference

  • Wikipedia – Regular expressions
  • Juejin. Cn/post / 684490…
  • Codepen. IO/code validation

More content, please poke my blog to understand, can leave a star better 💨