[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ja/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()

Baseline

20157

match() String

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);
// : Array ["T", "I"]

js
match(regexp)

regexp

Symbol.match

regexp RegExp RegExp new RegExp(regexp)

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

Array (g) 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) 0 input

match() global ignoreCase

g i 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 = "Nothing will come of nothing.";

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

match() [Symbol.match]() RegExp

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

regexp RegExp new RegExp(regexp)

js
const str1 =
  "All numbers except NaN satisfy <= Infinity and >= -Infinity in JavaScript.";
const str2 =
  "My grandfather is 65 years old and My grandmother is 63 years old.";
const 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"] 
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