How to define a regular expression

Define regular expressions as literals

ECMAScript supports regular expressions through the RegExp type. The syntax is as follows:

var expression = / pattern / flags;
Copy the code

Pattern can be any simple or complex regular expression that can contain character classes, qualifiers, grouping, lookup forward, and reverse application

Flags Specifies the behavior of regular expressions. The matching mode of a regular expression supports the following three flags:

  • g:Represents a global pattern that applies to all strings
  • i:Indicates case insensitive mode
  • m:Represents a multi-line pattern, that is, after reaching the end of a line, the search continues to see if there is an item matching the pattern on the next line

All metacharacters used in the schema must be escaped. Regular expression of yuan characters including: [{}]) \ ^ $|? * +. Etc

Define regular expressions in the form of RegExp constructors

The RegExp constructor takes two arguments: a string pattern to match and an optional flag string. Such as:

Var reg = new RegExp('[bc]at'.'i')
Copy the code

Because the RegExp constructor’s schema argument is a string, in some cases to double escape strings, all metacharacters must be double escaped, as must characters that have been escaped, for example, \n is usually escaped as \\n in object literals and becomes \\\\n in regular expression strings.

The difference between two expressions

In ECMAScript 3, regular expression literals always share the same RegExp instance, and every new RegExp instance created using the constructor is a new instance.

var re = null, i;
for (i = 0; i < 10; i++) {
  re = /cat/g;
  console.log(re.test('catastrophe'));
}
for (i = 0; i< 10; i++) {
  re = new RegExp('cat'.'g');
  console.log(re.test('catastrophe'))}Copy the code

In the first loop, only one RegExp instance is actually created for /cat/. Because the instance properties are not reset, the test() method will fail the next time the loop is called. This is because the first call to test() found ‘cat’, and the second call started with the ego with index 3, so it couldn’t. Because it tests to the end of the string, the next call starts from the beginning again.

In the second loop, since the RegExp instance is recreated each time through the loop, each call returns true

ECMAScript 5 states that when literals are created, one instance is created at a time, as when the RegExp constructor is used

RegExp instance property

  • global: Boolean value indicating whether it is setgmark
  • ignoreCase: Boolean value indicating whether it is setimark
  • lastIndex: integer, indicating the position in the string to start the search for the next match, starting from 0
  • multiline: Boolean value indicating whether it is setmmark
  • sourceReturns the pressliteralThe pattern string represented by the notation

RegExp instance method

exec()

The exec() method is used to retrieve a match for a regular expression in a string. The syntax regexpobject. exec(string) parameter

parameter describe
string A necessity. The string to retrieve
var text = 'mom and dad and baby'; var reg = /mom( and dad (and baby)?) ? /gi; console.log(reg.exec(text)) //[ // 0:"mom and dad and baby", / / 1:" and dad and baby", / / 2:"and baby",
//  groups: undefined,
//  index: 0,
//  input: "mom and dad and baby",
//  length: 3
//]
Copy the code

The return value

If the match is successful, the exec() method returns an array and updates the properties of the regular expression object. The array is returned with the fully matched text as the first item, followed by the successfully matched text in the re parentheses as an array.

Return value (array) describe
[0] All matched strings
[1], …[n ] Group capture in parentheses
index The matched character is at the 0 – based index of the original string
input Raw string

ES2018 introduced Named Capture Groups, which allows you to assign a name to each group match, making it easy to read the code and reference it.

const RE_DATE = /(? <year>\d{4})-(? <month>\d{2})-(? <day>\d{2})/; const matchObj = RE_DATE.exec('1999-12-31'); const year = matchObj.groups.year; // 1999 const month = matchObj.groups.month; // 12 const day = matchObj.groups.day; / / 31Copy the code

In the above code, the “named group match” is inside the parentheses, and the pattern header is “question mark + Angle bracket + group name” (?). That group name can then be referenced on the Groups attribute where the exec method returns the result. Meanwhile, numeric ordinal number (matchObj[1]) is still valid.

If the match fails, the exec() method returns NULL

test()

The test() method performs a search to see if the regular expression matches the specified string. Returns true or false. Syntax Regexpobject. test(string) parameter

parameter describe
string A string used to match a regular expression
let str = 'hello world! ';
let result = /^hello/.test(str);
console.log(result); 
// true
Copy the code

The return value

Returns true if the regular expression matches the specified string; False otherwise.

The toString () and toLocaleString ()

The toString() and toLocaleString() methods return regular string expressions in literal form

RegExp constructor property

The RegExp constructor contains properties that are considered static in other languages. These properties are accessed in two ways, a long property and a short property. The following table:

Long attribute name Short attribute name describe
input The $_ String that was last matched. Opera does not implement this property
lastMatch $& Last match. Opera does not implement this property
lastParen + $ The capture group that was last matched. Opera does not implement this property
leftContext $` The text before lastMatch in the input string
mutiline $* Boolean value indicating whether all expressions use multi-line mode. IE and Opera do not implement this property
rightContext $’ The text after lastMatch in the input string
var txt = 'this has been a short summer'; var reg = /(.) hort/g;if (reg.test(txt)) {
  console.log(RegExp.The $_); // 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   safari false || chrome undefined
}
Copy the code

In addition to the above attributes, there are nine constructor attributes for capturing groups. RegExp.$1… RegExp.$9 for first and second… Etc match capture group

Limitations of patterns

Regular expressions in ECMAScript lack advanced features supported by some languages, such as Perl. Some unsupported features are listed below. (To learn more, visit www.regular-expressions.info)

  • Matches the \A and \Z anchors at the beginning and end of the string
  • Look behind
  • Union and intersection classes
  • Atomic groups
  • Unicode support (except for single strings, such as: \uFFFF)
  • Named capture group
  • S (single, single line) and X (free-spacing, unspaced) match patterns
  • Matching conditions
  • Regular expression comments

If there is infringement, please send email to [email protected] or leave a message, I will contact you in the first time, thank you!!