string.matchAll()

matchAll() ๋ฉ”์†Œ๋“œ๋Š” ๊ทธ๋ฃน์บก์ณ๋ฅผ ํฌํ•จํ•˜์—ฌ ์ •๊ทœ์‹์— ๋Œ€ํ•œ ๋ฌธ์ž์—ด๊ณผ ์ผ์น˜ํ•˜๋Š” ๋ชจ๋“  ๊ฒฐ๊ณผ์˜ ๋ฐ˜๋ณต์ž(iterator) object๊ฐ€ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

let regexp = /t(e)(st(\d?))/g;
let str = 'test1test2';

let array = [...str.matchAll(regexp)];

/* 
str.matchAll(regexp)๋ฅผ console์— ์ฐ์œผ๋ฉด.. 
RegExpStringIterator {}
__proto__: RegExp String Iterator
next: ฦ’ next()
Symbol(Symbol.toStringTag): "RegExp String Iterator"
__proto__: Object 
*/

array[0]; // ["test1", "e", "st1", "1", index: 0, input: "test1test2", groups: undefined]
array[1]; // ["test2", "e", "st2", "2", index: 5, input: "test1test2", groups: undefined]

Syntax

str.matchAll(regexp)

Parameters

regexp

์ •๊ทœ ํ‘œํ˜„์‹ ๊ฐ์ฒด

๋น„ ์ •๊ทœ์‹ ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌํ•œ๋‹ค๋ฉด, ์ด๊ฒƒ์€ newExp(obj) ๋ฅผ ์ด์šฉํ•˜์—ฌ RegExp ๋กœ ๋ณ€ํ™˜๋œ๋‹ค.

RegExp ๊ฐ์ฒด๋Š” ๋ฌด์กฐ๊ฑด /g ํ”Œ๋ž˜๊ทธ๋ฅผ ๊ฐ€์ ธ์•ผ ํ•˜๋ฉฐ, ์•„๋‹ˆ๋ฉด TypeError ๋ฅผ ๋˜์ง„๋‹ค. (๋‹น์—ฐ All์ด๋‹ˆ๊นŒ)

Return value

iterator (์žฌ์‹œ์ž‘ ๋ฐ˜๋ณต์€ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค)

Examples

Regexp.exec() ์™€ matchAll()

MatchAll ๋ฅผ JavaScript์— ์ถ”๊ฐ€ํ•˜๊ธฐ ์ „์— ๋ชจ๋“  ์ผ์น˜ ํ•ญ๋ชฉ์„ ์–ป๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฃจํ”„์—์„œ regexp.exec(๋ฐ /g ํ”Œ๋ž˜๊ทธ๊ฐ€ ์žˆ๋Š” regexe) ํ˜ธ์ถœํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

const regexp = RegExp('foo[a-z]*', 'g');
const str = 'table foolball, foosball';
let match;

while ((match = regexp.exec(str)) !== null) {
  console.log(`Found ${match[0]} start=${match.index} end=${regexp.lastIndex}`);
}

// Found foolball start=6 end=14
// Found foosball start=16 end=24

MatchAll ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด while loop์™€ g ํ”Œ๋ž˜๊ทธ์™€ ํ•จ๊ป˜ exec ํ”ผํ•  ์ˆ˜ ์žˆ๋‹ค.

๋Œ€์‹ ์— matchAll ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋” ํŽธ๋ฆฌํ•œ array spread ๋‚˜ Array.from() , for...of ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ˜๋ณต์ž(iterator) ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

const regexp = RegExp('foo[a-z]*', 'g');
const str = 'table football, foosball';
const matches = str.matchAll(regexp);

for (const match of matches) {
  console.log(`Found ${match[0]} start=${match.index} end=${match.index + match[0].length}.`);
}

// Found football start=6 end=14.
// Found foosball start=16 end=24.

// ๋งค์น˜ ๋ฐ˜๋ณต์ž๋Š” for...of ๋ฐ˜๋ณต์ดํ›„ ๋‹ค ์จ๋ฒ„๋ฆฐ๋‹ค. (์žฌ์‹œ์ž‘ ๋ฐ˜๋ณต์€ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค)
// ์ƒˆ๋กœ์šด ๋ฐ˜๋ณต์ž๋ฅผ ๋งŒ๋“ค์–ด matchAll์„ ๋‹ค์‹œ ํ˜ธ์ถœํ•ด์•ผํ•œ๋‹ค.
Array.from(str.matchAll(regexp), m => m[0]);
//  ["football", "foosball"]

matchAll ์€ g ํ”Œ๋ž˜๊ทธ๊ฐ€ ์—†๋‹ค๋ฉด TypeError๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

const regexp = RegExp('[a-c]', '');
const str = 'abc';
str.matchAll(regexp);
// TypeError

matchAll ์€ regexp.exec() ์™€ ๋‹ฌ๋ฆฌ ๋‚ด๋ถ€์ ์œผ๋กœ regexp ๋ฅผ cloneํ•˜์—ฌ ๋งŒ๋“ ๋‹ค. lastIndex ๋Š” ๋ฌธ์ž์—ด์„ ์Šค์บ”ํ•  ๋•Œ ๋ณ€๊ฒฝ๋˜์ง€ ์•Š๋Š”๋‹ค.

const regexp = RegExp('[a-c]', 'g'); // regexp.lastIndex๋Š” 0
regexp.lastIndex = 1;

const str = 'abc';
Array.from(str.matchAll(regexp), m => `${regexp.lastIndex} ${m[0]}`);
//  ["1 b", "1 c"]

regexp.lastIndex = 0;
Array.from(str.matchAll(regexp), m => `${regexp.lastIndex} ${m[0]}`);
// ["0 a", "0 b", "0 c"]

lastIndex

์ฝ๊ธฐ ์“ฐ๊ธฐ๊ฐ€ ๊ฐ€๋Šฅํ•œ ์†์„ฑ, ๋งค์นญ์„ ์‹œ์ž‘ํ•  ์œ„์น˜๋ฅผ ์ง€์ •ํ•œ๋‹ค.

์บก์ณ ๊ทธ๋ฃน์— ๋” ์—‘์„ธ์Šค ํ–ฅ์ƒ (String.prototype.match() ๋ณด๋‹ค)

matchAll ์ด ์„ค๋“๋ ฅ ์žˆ๋Š” ์ด์œ ๋Š” ์บก์ณ ๊ทธ๋ฃน์— ํ–ฅ์ƒ๋œ ์ ‘๊ทผ์ด๋‹ค.

match() ๋Š” /g ํ”Œ๋ž˜๊ทธ์™€ ํ•จ๊ป˜ ์“ฐ์ผ ๋•Œ ์บก์ณ ๊ทธ๋ฃน์€ ๋ฌด์‹œ๋œ๋‹ค.

let regexp = /t(e)(st(\d?))/g;
let str = 'test1test2';

str.match(regexp); // ["test1", "test2"]

matchAll ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด, ์บก์ณ ๊ทธ๋ฃน์— ๋” ์‰ฝ๊ฒŒ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.

let regexp = /t(e)(st(\d?))/g;
let str = 'test1test2'
let array = [...str.matchAll(regexp)];

array[0]; 
/* ["test1", // ์™„์ „ํžˆ ๋งค์น˜
"e", // '(e)' ๋ถ€๋ถ„์— ์˜ํ•ด ๋งค์น˜
"st1", // '(st(\d?))' ๋งค์น˜
"1", // '(\d?)' ๋งค์น˜
index: 0,  // 0์—์„œ๋ถ€ํ„ฐ ์™„์ „ ๋งค์น˜๋œ ๋ฌธ์ž์—ด์ด ๋‚˜ํƒ€๋‚จ
input: "test1test2", // ์ž…๋ ฅ๋œ ์›๋ž˜ ๋ฌธ์ž์—ด
groups: undefined] 
*/

array[1]; // ["test2", "e", "st2", "2", index: 5, input: "test1test2", groups: undefined]

Browser compatibility

IE์™€ Safari์—์„œ ์ง€์› ์•ˆ๋จ.

๋ชจ๋ฐ”์ผ์—์„  Safari์™€ Samsung Internet์—์„œ ์ง€์›์ด ์•ˆ๋œ๋‹ค.

Last updated

Was this helpful?