Regular expression

A regular expression is an expression that matches a string based on certain rules. This expression usually consists of ordinary characters, metacharacters, other special characters, and markup characters.

Relevant concepts

metacharacters

Metacharacters have one or more special functions in regular expressions:

Other special characters

Markup characters

Instance Creation Mode

1. Literal

let expression=/pattern/flags

// Example: Match global 10-19
let pattern=/1[0-9]/g
Copy the code

The pattern in the double slash indicates the matching pattern, which is usually composed of ordinary characters and metacharacters. The flags outside the double slash, called the flag character, are used to control the behavior of the regular expression, whether it exists or not.


To match the metacharacter itself, backslash \ escape is required:

// Matches the first number globally
let pattern=/ [0-9]
// Match the first [0-9]
let pattern=/ / \ [0-9 \]
Copy the code

2. Constructor

let expression=new RegExp("pattern"."flags")

// Example: Match the global number
let pattern=new RegExp("[0-9]."."g")
Copy the code

Regexps can be created based on existing regular expression instances and modified:

const re1=/cat/g
const re2=new RegExp(re1)     // "/cat/g"
const re2=new RegExp(re1,"i") // "/cat/i"
Copy the code

Since the RegExp constructor takes a string as an argument, a secondary escape is needed to match the metacharacter itself:

// Matches the first number
let pattern=/ [0-9]
// Match the first [0-9]
let pattern=new RegExp("\ \ [0-9 \ \]")
Copy the code

Relevant example

// Matches the first non-number
let pattern=/^\d/
let pattern=/\D/

// Match all numbers
let pattern=/[0-9]/g

/ / 1. 100-200
let pattern1=/ [1] [0-9] [0-9] /
//2. Either numbers, lowercase letters, or uppercase letters. Lowercase letters have priority
let pattern2=/[0-9]|([a-z])|[A-Z]/
//3. A lowercase letter with a number
let pattern3=/[a-z][0-9]*/
//4. A lowercase letter followed by at least one 9
let pattern4=/[a-z][9]+/
//5. Lowercase letters appear at least 5-10 times
let pattern5=/ [a-z] {5, 10} /
//6. Lowercase letters appear at least once
let pattern6=/[a-z]{1,}/
//7. Start with a number and end with a lowercase letter
let pattern7=/^[0-9][a-z]$/
/ / 8. The Numbers
let pattern8=/ [^ 0-9]
//9. Special characters
let pattern9=/[^0-9a-zA-Z]/
Copy the code

Instance attributes

let pattern=/\[0-9\]/g
pattern.global      //true
pattern.ignoreCase  //false
pattern.lastIndex   / / 0
pattern.source      / / "\ [0-9 \]"
pattern.flags       //"g"
Copy the code

Instance methods

  • test()

    Receives a string to be matched, returning true if the string matches the pattern

    let text="000-00-0000."
    let pattern=/\d{3}-\d{2}-\d{4}/
    pattern.test(test)  //true
    Copy the code
  • exec()

    Receives a string to be matched, and if the character matches the pattern, returns an Array instance with two additional properties: index and input; If no match is found, null is returned. Index indicates the index position of the matched string in the original string. Input indicates the original string.

    If the match is successful, there are two cases. If the matching pattern does not capture a group (), it returns an element, the matched result; Otherwise return multiple elements:

    let text="cat bat fat"
    let pattern=/cat/
    pattern.exec(text)  //["cat", index: 0, input: "cat bat fat"]
    Copy the code
    let pattern=/cat (bat (fat))/
    let pattern=/cat/
    pattern.exec(text)  //["cat bat fat", "bat fat", "fat", index: 0, input: "cat bat fat"]
    Copy the code

    If the global tag is set, each call searches forward for the next match; Otherwise, only the first match is always returned.

    let text="cat bat fat"
    let pattern=/.at/
    let matches=pattern.exec(text)
    matches.index       / / 0
    matches[0]          //"cat"
    pattern.lastIndex   / / "0"
    Copy the code
    let text="cat bat fat"
    let pattern=/.at/g
    let matches=pattern.exec(text)
    matches.index       / / 0
    matches[0]          //"cat"
    pattern.lastIndex   / / "3"
    
    // Second call
    matches=pattern.exec(text)
    matches.index       / / 4
    matches[0]          //"bat"
    pattern.lastIndex   / / "7"
    
    // Third call
    matches=pattern.exec(text)
    matches.index       / / 8
    matches[0]          //"fat"
    pattern.lastIndex   / / "11"
    
    // Fourth call
    matches=pattern.exec(text) //null
    Copy the code

RegExp constructor property

The RegExp constructor also has properties of its own that apply to all regular expressions in scope. These characters can be extractedexec()andtest()Information about the operation performed.

let text="this has been a short summer"
// Search for the character followed by HORT
let pattern=/ (.). hort/g
if(pattern.test(text)){
  console.log(RegExp.input) //this has been a short summer
  console.log(RegExp.lastMatch) //short
  console.log(RegExp.leftContext) //this has been a 
  console.log(RegExp.rightContext)  // summer
  console.log(RegExp.lastParen) //s
}
Copy the code

Abbreviations are mostly illegal ECMAScript identifiers, Opera does not support abbreviations, and IE does not support multi-line matching