Detailed explanation of the match method of String in JavaScript

String.prototype.match()

** The String.prototype.match() method returns the string result matched by a regular expression. **

var paragraph = 'The quick brown fox jumps over the lazy dog. It barked.';
var regex = /[A-Z]/g;
var found = paragraph.match(regex);

console.log(found);
// 输出: Array ["T", "I"]

grammar

str.match(regexp)

parameter

regexp
A regular expression object. If a non-regular expression object is passed, the function will execute new RegExp(obj) to convert it into a string object. If no arguments are passed, the function will return an empty string array: [""].

return value

If there is a matching value, String.prototype.match() returns an array whose contents depend on whether there is a g modifier in the regular expression parameter. Returns null if there is no matching value.
(1) If there is a g modifier in the regular expression. All matched values ​​will be returned in array format, and capture groups will not be returned.
(2) If there is no g modifier. Only the first matching value will be returned, and all capturing groups inside the first matching value will also be returned. In this case, the return value will include the following additional properties:

  • groups: If the matching value contains a capture group, the return value will contain the values ​​of the capture group information. Otherwise undefined.
  • index: The index of the matching value in the entire string.
  • input: the entire string.

describe

If the regular expression of the String.prototype.match() parameter does not contain the g modifier, str.match() will return the same result as the RegExp.prototype.exec() method.

  • If you just want to know whether a string matches a regular expression, use RegExp.prototype.test().
  • If you want to use a capture group in a regular expression match that includes the g modifier, use RegExp.prototype.exec().

example

Use String.prototype.match() directly

In the following example, String.prototype.match() will look for a string starting with "Chapter", followed by one or more numeric characters, followed by another or more '.+numeric characters'. In the following code, there is an i modifier in the regular expression, so the entire regular expression ignores case.

var str = 'For more information, see Chapter 3.4.5.1';
var re = /see (chapter \d+(\.\d)*)/i;
var found = str.match(re);

console.log(found);
// 输出 [ 'see Chapter 3.4.5.1', 'Chapter 3.4.5.1', '.1',
//        index: 22,
//        input: 'For more information, see Chapter 3.4.5.1' ]

// 'see Chapter 3.4.5.1'是整个匹配项值.
// 'Chapter 3.4.5.1' 被捕获组'(chapter \d+(\.\d)*)'捕获.
// '.1' 被最后一个捕获组'(\.\d)捕获'.
// 'index'属性(值22)是第一个匹配字符在整个字符串中的索引位置.
// 'input'是整个字符串.

Using the g modifier in String.prototype.match()

In the following example, the parameter regular expression contains the g modifier, and the result will return all matched values.

var str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
var regexp = /[A-E]/gi;
var matches_array = str.match(regexp);

console.log(matches_array);
// ['A', 'B', 'C', 'D', 'E', 'a', 'b', 'c', 'd', 'e']

No parameters are passed in String.prototype.match()

var str = "Nothing will come of nothing.";

str.match();   // 返回 [""]

a non-regular expression as an argument

When a parameter is a string or a number, the function will use new RegExp(obj) to convert it into a regular expression object. If the parameter is a positive number containing a "+" sign, the RegExp() function will ignore the "+" sign.

var str1 = "NaN means not a number. Infinity contains -Infinity and +Infinity in JavaScript.",
    str2 = "My grandfather is 65 years old and My grandmother is 63 years old.",
    str3 = "The contract was declared null and void.";
str1.match("number");    // "number" 是个字符串,返回["number"]
str1.match(NaN);         // NaN 的数据类型是数字,返回 ["NaN"]
str1.match(Infinity);    // Infinity 的数据类型是数字,返回 ["Infinity"]
str1.match(+Infinity);   // 返回 ["Infinity"]
str1.match(-Infinity);   // 返回 ["-Infinity"]
str2.match(65);          // 返回 ["65"]
str2.match(+65);         // “+”符号将会被忽略,返回 ["65"]
str3.match(null);        // 返回 ["null"]

Guess you like

Origin blog.csdn.net/yuhk231/article/details/88191846