[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/match [Back]  [Original]

String.prototype.match() - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

String.prototype.match()

20157

match()

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

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

js
match(regexp)

regexp

Symbol.match

regexp RegExp Symbol.match new RegExp(regexp) RegExp

match() [""] match(/(?:)/)

Arrayg null

String.prototype.match Symbol.match RegExp.prototype[Symbol.match]()

match() RegExp.prototype[Symbol.match]()

match()

match "Chapter" 1 0 i

js
const str = "For more information, see Chapter 3.4.5.1";
const re = /see (chapter \d+(\.\d)*)/i;
const 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',
//   groups: undefined
// ]

'see Chapter 3.4.5.1' 'Chapter 3.4.5.1' (chapter \d+(\.\d)*) .1 (\.\d) index (22) input

match()

match() A E a e

js
const str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
const regexp = /[A-E]/gi;
const matches = str.match(regexp);

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

"fox" "cat" animal

js
const paragraph = "The quick brown fox jumps over the lazy dog. It barked.";

const capturingRegex = /(?<animal>fox|cat) jumps over/;
const found = paragraph.match(capturingRegex);
console.log(found.groups); // {animal: "fox"}

match()

js
const str = "";

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

[Symbol.match]() RegExp match()

Symbol.match Symbol.match match()

js
const str = "Hmm, this is interesting.";

str.match({
  [Symbol.match](str) {
    return ["Yes, it's interesting."];
  },
}); // returns ["Yes, it's interesting."]

regexp new RegExp(regexp) RegExp

js
const str1 =
  "NaN  JavaScript  Infinity  -Infinity  +Infinity";
const str2 = " 65  63 ";
const str3 = " null  void";
str1.match(""); //  [""]
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"]

js
console.log("123".match("1.3")); // [ "123" ]

.

js
console.log("123".match("1\\.3")); // null

ECMAScript 2027 LanguageSpecification
# sec-string.prototype.match


Web Proxy Viewer  |  New URL  |  Original Page