Most of the time, we have the impression that regular expressions are hard to learn and hard to remember.
What is a regular expression?
Regular Expression uses a single character to describe and match a sequence that matches a particular syntaxThe rulesString of.
The purpose of using regular expressions isreplaceWork. Define a rule for yourself, and then go into the string and match the substrings that match that rule.
To better understand regular expressions, we can use visualization tools at:

Regexpert: regexper.com Regulex: jex.im/ Regulex /#! F… Debuggex:www.debuggex.com/

Learn the syntax of regular expressions:

1. RegExp objects

Js supports regular expressions through the built-in RegExp object, and there are two ways to instantiate a RegExp object: a. Literals b. constructors
A. literal
var reg = /\bare\b/g;Copy the code

Write the picture description here

How to perform full-text matching? ☞”g

Write the picture description here

B. Constructor
var reg = new RegExp('\\bare\\b'.'g');Copy the code
The first argument is the text of the string ☞ regular expression. The “\” character in js is itself a special character, which needs to be escaped if you want to use it.
Second argument: string ☞ identifier.

Write the picture description here

Full text match –> “g” :

Write the picture description here

The modifier
G: global– Full text search, no addition, search until the first match;
I: Ignore case– ignore case, default case sensitive;
M: Multiple lines– multiple lines of search

Write the picture description here


Write the picture description here

Found: uppercase word not replaced, want to ignore case, match “I” regardless of case.

Write the picture description here

2. Metacharacters

Regular expressions consist of two basic character types: literal characters and metacharacters
Metacharacters are non-alphabetic characters that have special meanings in regular expressions
* +? $^. | \ () {} []

Write the picture description here

3. Character classes

Typically, one character in a regular expression corresponds to one character in a string. Sometimes, if you want to match a character of a certain class (that is, a character of a certain class that matches a set of characteristics), what should you do?
☞ We can use the metacharacter [] to construct a simple class;
▌ The so-called class refers to objects that conform to certain characteristics, and is a general reference rather than a particular character.
☞ Expression [ABC] Sets characters A, B, or C into a class that an expression can match.

Write the picture description here

The character class is inverted
A. Use metacharacters^createReverse class/negative class
B. Reverse classes refer to content that does not belong to a class
C. The expression [^ ABC] represents something other than a or B or c

Write the picture description here

4. Scope classes

Use character classes to match numbers [0123456789]
You can use[a-z]To concatenate 2 characters, representingAny character from a to Z(This is a closed interval, which includes a and z themselves).

Write the picture description here


Write the picture description here

Inside the class formed by [] is the conjunctive [a-za-z].

Write the picture description here


Write the picture description here

Q: “-” is not a special character, nor is it a metacharacter. What happens when you just want to match “-” in a class?

Write the picture description here

Remember: “-” in the middle, i.e. a beginning and an end, indicates range.

Write the picture description here

This will match the “-“.

5. Predefined classes

Write the picture description here

Match aAb + number + any characterThe string

Write the picture description here


Write the picture description here

Regular expressions also provide several common onesThe borderA match

Write the picture description here


Write the picture description here


Write the picture description here


Write the picture description here


Write the picture description here


Write the picture description here

Discovery: Everywhere “is” has been replaced.
Just replace the word “is”. Words have word boundaries.

Write the picture description here





Write the picture description here

Note:The meaning of metacharacters is not unique and has different meanings in different scenarios. The meaning not in [] is not inverted, but “starts with xx”.

Write the picture description here

“.@ “☞ Match any character +@
“@.” ☞ Match @+ any character
“.@$” ☞ “only @ as the end” is matched

6, quantifiers

We want to match a succession of n number of strings, such as “\ d \ d \ d \ d \ d \ d \ d \ d \ d \ d…” In order to solve this problem, regular expressions introduce the concept of quantifiers.

Write the picture description here


Write the picture description here


Write the picture description here


Write the picture description here


Write the picture description here


Write the picture description here


Write the picture description here


Write the picture description here


Write the picture description here


Write the picture description here

Greedy mode and non-greedy mode of regular expressions

A. Greed mode

When a regular expression is matchedMatch as many as you can, until the match fails, the default is greedy mode.

Write the picture description here

B. Non-greedy mode

Let the regular expressionMatch as few as possible, that is, once the match is successful, it doesn’t go any further, which is the non-greedy mode. ☞ inAfter the quantifier?Can.

Write the picture description here


Write the picture description here

8, group

Matches three consecutive occurrences of the string javascript, if javascript{3} is written like this, as follows

Write the picture description here

A quantifier can only be used for the letter next to it, not as a whole word. use( )The function of grouping can be achieved, so that quantifiers act on groups.
(javascript) {3}, as follows

Write the picture description here


Write the picture description here

use|Can achieveorThe effect of:

Write the picture description here


Write the picture description here


Write the picture description here


Write the picture description here

backreferences

The 2017-11-10 = > 11/10/2017

Write the picture description here


Write the picture description here

What happens when you replace a variable instead of a constant? ☞ “$” Group the contents of the class.

Write the picture description here


Write the picture description here

Use “$1” and so on to represent the capture of the group, also known as the group capture.
How about using a grouping, but not wanting to capture it? ☞ Ignore grouping
You don’t want to capture certain groups, just add? : can.

Write the picture description here


Write the picture description here

9, vision

Regular expressions are parsed from the text header to the tail, which is called “front”, and the text header, which is called “back”.
Lookahead is when a regular expression matches a rule, it checks forward to see if it matches the assertion, and backward/backward to see if it matches the assertion.
Js does not support backwardness.
Conforming to a specific assertion is calledPositive/positiveMatching; Failure to conform to a particular assertion is calledNegative/negativeMatching.

Write the picture description here


Write the picture description here


Write the picture description here


Write the picture description here

Js object properties

Global: Indicates whether to search for the full text. The default value is false.
Ignore case: whether the case is case sensitive. The default value is false.
Multiline: multi-line search, false by default.
LastIndex: is the position next to the last character of the current expression matching content.
Source: The text string of the regular representation.
var reg1 = /\w/;
var reg2 = /\w/gim;Copy the code

Write the picture description here


Write the picture description here


Write the picture description here

RegExp. Prototype. test(STR)☞ Used to test whether the string argument matches the regular expression pattern. Return true if it exists, false otherwise.
var reg1 = /\w/;
var reg2 = /\w/g;Copy the code

Write the picture description here

Cause: Influenced by lastIndex.
var reg1 = /\w/;
var reg2 = /\w/g;
while(reg2.test('ab')){
    console.log(reg2.lastIndex);
}Copy the code

Write the picture description here


Write the picture description here

You only get it right the first time, so you instantiate a new one every time, but there’s a memory overhead. There’s no need to do that.

Write the picture description here

RegExp. Prototype. exec(STR)☞ The string is searched using the regular expression pattern and the properties of the global RegExp object are updated to reflect the match.
Returns null if there is no matching text, otherwise returns an array of results:
indexDeclares the position of the first character of the matching text
inputHolds the retrieved string string

Non-global call

When exec() of a non-global RegExp object is called, the array is returned;
The first element is the text that matches the regular expression;
The second element is the text (if any) that matches the first subexpression of RegExpObject;
The third element is the text (if any) that matches the second subexpression of the RegExp object, and so on.
String.prototype.search(reg)
▌ The ☞search() method is used to retrieve the substring specified in the string, or the substring that matches the regular expression;
☞ The index method returns the first matching result, -1 if not found;
The ☞ Search () method does not perform a global match, which ignores the flag G and always retrieves from the beginning of the string.
String.prototype.match(reg)
▌ The match() method retrieves the string to find one or more text that matches the regexp;
▌ Whether regexp has the flag G or not greatly affects the result.
Non-global call
If regexp does not flag g, the match() method matches only once in the string; If no matching text is found, it returns NULL; otherwise, it returns an array containing information about the matching text it found; The first element of the returned array holds the matching text, while the remaining elements hold the text that matches the subexpression of the regular expression. In addition to the regular array elements, the returned array contains two object attributes:indexDeclares the position of the starting character in the string for the matching text;inputThe statement ofstringObjectThe reference.
The global call
If regexp has the flag G, match() performs a global search to find all matching substrings in the string, or returns null if no matching strings are found. If one or more matching strings are found, an array is returned.
The array element holds all the matching substrings in the string and has no index or input attribute.
String.prototype.split(reg)
Split () is often used to split a string into an array of characters: ‘a,b,c,d’.split(‘,’); //[“a”,”b”,”c”,”d”]
Complex split cases can also be solved using regular expressions: ‘a1b2c3d’.split(/\d/); //[“a”,”b”,”c”,”d”]
String.prototype.replace
☞ String. The prototype. The replace (STR, replaceStr)
☞ String. The prototype. The replace (reg, replaceStr)
☞ String. The prototype. The replace (reg, function)

Write the picture description here

Function Parameter Meaning
Function is called every time a replacement is matched and takes four arguments:
1. Match the string
2. The regular expression group content. If the regular expression does not group content, this parameter is not available
3. Index of the matching item in the string
4. The original string

Write the picture description here


Write the picture description here