javascript Type System_Regular Expression RegExp Type Details

  • 2021-06-29 10:24:23
  • OfStack

Previous remarks

The basic syntax of regular expressions in javascript has been described earlier.The RegExp class of javascript represents regular expressions, and both String and RegExp define methods that allow powerful pattern matching and text retrieval and substitution.This article describes the RegExp object for regular expressions and the implications of regular expressions
Attributes and methods to

object

The regular expression in javascript is represented by an RegExp object and has two ways of writing: one is literal quantification;The other is constructor writing

Perl Writing

Regular expression literal quantifier, also known as Perl, because the regular expression feature of javascript is derived from Perl

Regular expression literal quantities are defined as characters contained between a pair of slashes (/) and can be set with three flags

var expression = /pattern/flags;

The matching pattern of the regular expression supports the following three flags:

g: Represents a global (global) pattern, that is, the pattern will be applied to all strings instead of stopping immediately when the first match is found

i: Indicates a case-insensitive (case-insensitive) pattern that ignores the case of patterns and strings when determining matches

m: Represents a multiline (multiline) pattern in which you continue to look for items matching the pattern on the next line when you reach the end of a line of text


// Match string all 'at' Examples 
var p = /at/g;
//test() Method Return 1 Boolean values indicate whether a match can be found 
console.log(p.test('ata'));//true
console.log(p.test('aba'));//false

RegExp constructor

Like normal built-in object 1, the RegExp regular expression object also supports the form of the new+RegExp() constructor

The RegExp constructor takes two parameters: the string pattern to match (pattern) and the optional flag string (flags), which has the same meaning as the three flags of the literal quantity:'g','i','m'

Both parameters of the RegExp constructor are strings.And any expression defined in literal form can use a constructor


// Match string all 'at' Examples 
var p1 = /at/g;
// Ditto 
var p2 = new RegExp('at','g');

[Note] The ECMAScript3 specification specifies that a regular expression direct quantity is converted to an RegExp object when it is executed and that each operation of the regular expression direct quantity represented by the same code returns the same object.The ECMAScript5 specification does the opposite, each time the direct amount of a regular expression is represented by a code

All operations return new objects.IE6-81 has been implemented in accordance with the ECMAScript5 specification, so there are no compatibility issues

Since regular expression literals do not support variables, if variables appear in regular expressions, they can only be stitched into the parameters of the RegExp constructor using the RegExp constructor as a string stitch.

[tips] Get elements by class name classname


function getByClass(obj,classname){
  var elements = obj.getElementsByTagName('*');
  var result = [];
  var pattern = new RegExp( '(^|\\s)'+ classname + '(\\s|$)');
  for(var i = 0; i < elements.length; i++){
    if(pattern.test(elements[i].className)){
      result.push(elements[i]);
    }
  }
  return result;
}

Instance Properties

Each RegExp instance object contains the following five properties


global: *    Boolean value indicating whether set g sign 
ignoreCase:  Boolean value indicating whether set i sign 
lastIndex:   Integer indicating the start of the search 1 Character position of matches, from 0 Calculate from 
multiline:   Boolean value indicating whether a flag is set m
source:  *   The string representation of a regular expression, returned as a literal quantity instead of a string pattern passed into the constructor 

var pattern = new RegExp('\\[bc\\]at','i');
console.log(pattern.global);//false
console.log(pattern.ignoreCase);//true  
console.log(pattern.multiline);//false
console.log(pattern.lastIndex);//0
console.log(pattern.source);//'\[bc\]at'

If exec() or test() functions of RegExp are used and the global mode'g'is set, the matching of regular expressions starts at the position of lastIndex and resets lastIndex after each unsuccessful mismatch.In this way, you can iterate over and over the string, searching for each matching result in turn.However, if the exec () or test () methods of the same RegExp need to be called on different strings, this variable may also have unexpected matching results, so explicitly set lastIndex of RegExp to zero when replacing strings


//exec() Method returns a match as an array 
var p = /\w/g;
var s = 'ab';
console.log(p.lastIndex);//0
console.log(p.exec(s));//['a']
console.log(p.lastIndex);//1
console.log(p.exec(s));//['b']
console.log(p.lastIndex);//2
console.log(p.exec(s));//null
console.log(p.lastIndex);//0

var p = /\w/g;
var s1 = 'ab';
var s2 = 'ba';
console.log(p.lastIndex);//0
console.log(p.exec(s1));//['a']
console.log(p.lastIndex);//1
console.log(p.exec(s2));//['a']
console.log(p.lastIndex);//2

Constructor Properties

RegExp constructor properties are treated as static properties that vary based on the last regular expression operation performed

There are two ways to access them, long and short attribute names.Most short attribute names are not valid ECMAScript identifiers, so they must be accessed through square bracket syntax


 Long attribute name      Short attribute name      *     Explain 
input       $_         Lately 1 Secondary matching string 
lastMatch     $&         Lately 1 Second Match 
lastParen     $+         Lately 1 Secondary Matching Capture Group 
leftContext    $`        input In string lastMatch Previous Text 
multiline     $*         Boolean value indicating whether all expressions use multiline mode 
rightContext   $'        Input In string lastMarch Text after 

Using these properties, you can extract more specific information from operations performed by the exec () method or the test () method


//test() For testing 1 Whether or not a string matches a regular expression and returns 1 Boolean values 
var text = 'this has been a short summer';
var pattern = /(.)hort/g;
if(pattern.test(text)){
  console.log(RegExp.input);//'this has been a short summer'
  console.log(RegExp.leftContext);//'this has been a '
  console.log(RegExp.rightContext);//' summer'
  console.log(RegExp.lastMatch);//'short'
  console.log(RegExp.lastParen);//'s'
  console.log(RegExp.multiline);//false
  console.log(RegExp['$_']);//'this has been a short summer'
  console.log(RegExp['$`']);//'this has been a '
  console.log(RegExp["$'"]);//' summer'
  console.log(RegExp['$&']);//'short'
  console.log(RegExp['$+']);//'s'
  console.log(RegExp['$*']);//false    
}

javascript has nine constructor properties for storing capture groups that are automatically populated when the exec () or test () methods are called

[Note] In theory, RegExp. $0, which should hold the entire expression-matching text, does not exist and has an undefined value


//RegExp.$1\RegExp.$2\RegExp.$3 ...to RegExp.$9 Used separately for storing 1.  No. 2 ... 9 Matching capture groups 
var text = 'this has been a short summer';
var pattern = /(..)or(.)/g;
if(pattern.test(text)){
  console.log(RegExp.$1);//sh
  console.log(RegExp.$2);//t
}

Instance Method

There are five instance methods for the RegExp object, which are divided into two categories.Includes three object generic methods toString(), toLocalString(), valueOf() and test(), exec() regular matching methods

Object Common Methods

The RegExp object inherits the common methods toString(), toLocaleString(), and valueOf() of the Object object.

[toString()]

The toString() method returns the literal amount of a regular expression

[toLocaleString()]

The toLocaleString() method returns the literal amount of a regular expression

[valueOf()]

The valueOf() method returns the return regular expression object itself

[Note] Regardless of how regular expressions are created, all three methods return only their literal form


// Match string all 'at' Examples 
var p1 = /at/g;
// Ditto 
var p2 = new RegExp('at','g');
0

Regular Matching Method

There are only two regular matching methods for the regular expression RegExp object: exec() and test().

[exec()]

The exec() method is designed specifically for capturing groups and accepts one parameter, the string to which the pattern is applied.It then returns an array containing match information and null without a match

In the Matches array, item 1 is a string that matches the entire pattern, and item 1 is a string that matches the capture group in the pattern. If there is no capture group in the pattern, the array contains only one item

The returned array contains two additional properties: index and input.index denotes the position of the match in the string, input denotes the string to which the regular expression is applied


// Match string all 'at' Examples 
var p1 = /at/g;
// Ditto 
var p2 = new RegExp('at','g');
1

For the exec() method, even if the global flag (g) is set in the pattern, it will only return one match at a time.Calling exec () multiple times on the same string without setting a global flag always returns information for the first match;With a global flag set, each call to exec() continues to find a new match in the string


// Match string all 'at' Examples 
var p1 = /at/g;
// Ditto 
var p2 = new RegExp('at','g');
2

var text = 'cat,bat,sat,fat';
var pattern2 = /.at/g;
var matches = pattern2.exec(text);
console.log(pattern2,matches);  
//pattern2.lastIndex:3
//matches[0]:'cat'
//matches.index:0
//matches.input:'cat,bat,sat,fat'

var text = 'cat,bat,sat,fat';
matches = pattern2.exec(text);
console.log(pattern2,matches);  
//pattern2.lastIndex:7
//matches[0]:'bat'
//matches.index:4
//matches.input:'cat,bat,sat,fat'

[tips] Use the exec() method to find all matching locations and all values


// Match string all 'at' Examples 
var p1 = /at/g;
// Ditto 
var p2 = new RegExp('at','g');
4

[test()]

The test() method is used to test whether a regular expression can find matching text in a string, accepts a string parameter, returns true when matching, or returns false otherwise


// Match string all 'at' Examples 
var p1 = /at/g;
// Ditto 
var p2 = new RegExp('at','g');
5

Similarly, when the test() method is called, the lastIndex property of the RegExp object changes.If global mode is specified, every time the test() method is executed, an attempt to match starts with the lastIndex offset value in the string, so different strings are validated multiple times with the same RegExp, and the lastIndex value must be set to zero after each call


// Match string all 'at' Examples 
var p1 = /at/g;
// Ditto 
var p2 = new RegExp('at','g');
6

As mentioned earlier, javascript has nine constructor properties for storing capture groups that are automatically populated when the exec () or test () methods are called

[Note] In theory, RegExp. $0, which should hold the entire expression-matching text, does not exist and has an undefined value


// Match string all 'at' Examples 
var p1 = /at/g;
// Ditto 
var p2 = new RegExp('at','g');
7

Related articles: