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
Parameters
regexp
์ ๊ท ํํ์ ๊ฐ์ฒด
๋น ์ ๊ท์ ๊ฐ์ฒด๋ฅผ ์ ๋ฌํ๋ค๋ฉด, ์ด๊ฒ์ newExp(obj)
๋ฅผ ์ด์ฉํ์ฌ RegExp
๋ก ๋ณํ๋๋ค.
RegExp
๊ฐ์ฒด๋ ๋ฌด์กฐ๊ฑด /g
ํ๋๊ทธ๋ฅผ ๊ฐ์ ธ์ผ ํ๋ฉฐ, ์๋๋ฉด TypeError
๋ฅผ ๋์ง๋ค. (๋น์ฐ All์ด๋๊น)
Return value
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์์ ์ง์์ด ์๋๋ค.