JS regular expression introduction

A regular expression is a string that matches a certain rule.

Regular expression tool: regexper.com/

The RegExp object

To instantiate RegExp:

  • literal

    Var reg = /\bis\b/g // \b stands for word boundary

    ‘he is a boy. This is a dog.’.replace(reg, ‘IS) //he IS a boy. This IS a dog.

  • The constructor

    var reg = new RegExp(‘\bis\b’, ‘g); ‘he is a boy. This is a dog.’.replace(reg, ‘IS) //he IS a boy. This IS a dog.

The modifier

  • G: Global full text search, do not add, search until the first match stops
  • I: Ignre case ignores case and is case sensitive by default
  • M: Multiple lines search (including newlines)

metacharacters

Metacharacters are non-alphabetic characters that have special meanings in regular expressions

* +? $^. | \ () {} []

  • \ T: horizontal TAB character
  • \ V: vertical TAB character
  • \ n: a newline character
  • \ r: a carriage return
  • \ 0: null character
  • \ f: form-feed character
  • \cX: Control character corresponding to X (Ctrl + X)

Character classes

Use the metacharacter [] to build a simple class

[ABC] indicates that characters a or B or C are grouped together, and expressions can match such charactersCopy the code

Reverse character classes (use metacharacters ^ to create reverse/negative classes)

[^ ABC] indicates something other than character A or b or CCopy the code

Scope of the class

[A-z] indicates any character from a to Z. [A-za-z0-9] indicates a-z a-z 0-9Copy the code

Predefined classes

Example: Match a string ab[0-9][^\r\n] ab\d with an ab + a number + an arbitrary character.Copy the code

The border

quantifiers

Matches a string that occurs more than once in a row

Greed mode

Regular expressions are matched as much as possible

\d{3,6} to match 12345678 // 123456Copy the code

Non-greedy model

Make the regular expression match as little as possible,

'123456789'. The match (/ \ d {3, 5}? /g) // ['123', '456', '789']Copy the code

grouping

Groups can be achieved by using (). Apply quantifiers to groups

The matching string Byron appears 3 times in a row Byron{3} // false it indicates n appears 3 times in a row (Byron) {3} // correctCopy the code

Or (|)

Byron | Casper     

Byr(on|Ca)sper

backreferences

2020-10-12 => 10/12/2020

'2020-10-12'.replace(/(\d{4})-(\d{2})-(\d{2})/g, '$2/$3/$1')
// '10/12/2020'
Copy the code

Ignore the grouping

You don’t want to capture certain groups, just add? :

(? : Byron). (ok) $1 is okCopy the code

foresight

The regular expression is parsed from the head of the text to the tail of the text, called the “front”

Lookahead is when a regular expression matches a rule, and the lookback/lookback direction is the opposite (JS does not support lookback)

'a2*3'.replace(/\w(? =\d)/g, 'X') // X2*3 'a2*v3vv'.replace(/\w(? =\d)/g, 'X') //'X2*X3vv'Copy the code

Js RegExp method

  • RegExp.prototype.test(str)

Returns Boolean that tests whether a string matching the regular expression pattern exists in a string argument

  • 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 a result array;

  • Index declares the position of the first character of the matching text
  • Input holds the retrieved string string