๐Ÿ“
til
  • TIL(Today I Learned)
  • javascript
    • value-number-string-boolean-null-undefined
    • primitive-reference
    • Hoisting
    • Prototypes in Javascript
    • this
    • prototype
    • ์ฝœ๋ฐฑํ•จ์ˆ˜ (Callback function)
    • ํ•จ์ˆ˜์˜ ํ˜ธ์ถœ
    • ์ž๋ฃŒ๊ตฌ์กฐ new keyword
    • closure
    • Promise
    • event-loop
    • array-object
    • ๊ฐ์ฒด
    • Arguments
    • higher order function
    • operators-function-control-flow
    • ๊ฐ์ฒด ์ƒ์„ฑ ํŒจํ„ด 3๊ฐ€์ง€
    • Javascript scopes
    • Functional Programming
    • Design Patterns
    • ๋ฐ์ดํ„ฐ ํƒ€์ž…
    • Object ๊ฐ์ฒด
    • ํ‘œ์ค€ ๋‚ด์žฅ ๊ฐ์ฒด์˜ ํ™•์žฅ
    • ์ฐธ์กฐ
    • ํ•จ์ˆ˜
    • ์ƒ์†(Inheritance)
    • this - 'this'๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํ•ด๋‹น ํ•จ์ˆ˜๋ฅผ '์–ด๋–ป๊ฒŒ' ์‹คํ–‰ํ•˜๋А๋ƒ์— ๋”ฐ๋ฅธ 4๊ฐ€์ง€ this ์ •์˜
    • ์ „์—ญ๊ฐ์ฒด(Global object)
    • ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ
    • The 'new' keyword - Object Creation in JavaScript
  • javascript-api
    • Number
      • Number.MAX_VALUE
      • Number.isInteger
      • Number.NEGATIVE_INFINITY
      • Number.isNaN()
      • Number.POSITIVE_INFINITY
      • Number.parseFloat
      • Number.EPSILON
      • number.toExponential
      • Number.MAX_SAFE_INTEGER
      • Number1 - ์ž์—ฐ์ˆ˜, ์ •์ˆ˜, 10์ง„์ˆ˜, 2์ง„์ˆ˜, ๋ถ€๋™์†Œ์ˆ˜์ , ์‹ค์ˆ˜
      • Number.isSafeInteger()
      • Number.MIN_VALUE
      • Number.parseInt
      • Number.NaN
      • Number.isFinite()
      • Number.MIN_SAFE_INTEGER
      • toFixed
    • string.split
    • String.fromCodePoint
    • string.trimEnd
    • string.padStart
    • string.@@iterator
    • String.fromCharCode
    • string.toUpperCase
    • string.codePointAt
    • string.toLowerCase()
    • string.toString
    • string.includes
    • string.replace()
    • string.charAt
    • String.lastIndexOf
    • string.slice
    • string.search
    • string.padEnd
    • string.substring
    • string.length
    • string.trim
    • string.localeCompare
    • String.indexOf
    • string.endsWith
    • string.valueOf
    • String.raw
    • string.matchAll()
    • string.repeat
    • string.match
    • String.prototype
    • string.startsWith
    • string.charCodeAt
    • string.trimStart
    • string.concat
    • string.toLocaleUpperCase()
    • string.toLocaleLowerCase
    • String
  • learn-node
    • debugger
    • Tip
  • schema-design
    • Database Schema Design
    • Database Schema Design
  • react
    • LifeCycle
    • redux
    • Context API
    • ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์™€ ํด๋ž˜์Šค, ์–ด๋–ค ์ฐจ์ด๊ฐ€ ์กด์žฌํ• ๊นŒ?
    • Hooks๊ณผ useEffect ์‚ฌ์šฉํ•ด ๋ณด๊ธฐ
    • Route
    • async wait ์‚ฌ์šฉํ•˜๊ธฐ
    • Hooks API Reference
    • context
    • npm uninstall ํ•˜๋Š”๋ฒ•
    • test ๋งŒ๋“ค๊ธฐ
  • tip
    • ํด๋ฆฐ์ฝ”๋“œ
    • BxSlider๋กœ ํ…์ŠคํŠธ ํ๋ฅด๋Š” ํšจ๊ณผ ๋งŒ๋“ค๊ธฐ
  • javascript30
    • Event Capture, Propagation, Bubbling and Once
    • Object and Arrays - Reference VS Copy
  • typescript
    • ์šฐ์•„ํ•œ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ 2๋ถ€
    • The Basic Cheatsheet
    • TypeScript
    • Type Guards and Differnetiating Types
    • ์šฐ์•„ํ•œ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ
    • Generics
  • git-from-the-hell
    • git
    • init-status-add-commit-log-stage-repository
    • log-diff
    • ๋จธ์ง€ ํ›„ branch ์‚ญ์ œํ•˜๊ธฐ
    • ์ง€์˜ฅ์—์„œ ์˜จ Git
    • reset-revert
    • develop branch ๋ฅผ pull ํ•˜๊ณ  ์‹ถ์„๋•Œ
  • conference-and-seminar
    • ๋ชจ๋˜ ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœํ™˜๊ฒฝ์˜ ์ดํ•ด
  • fire-base
    • Firebase .gitignore
  • vanillacoding
    • Data Structures
    • Sorting Algorithms - part 1
    • Promise
    • class
    • 04.quiz
    • 07.event-loop
    • Design Patterns
    • OOP (Object Oriented Programming)
  • etc
    • ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ฝ”๋“œ๋ฆฌ๋ทฐ
    • ์Šค์Šค๋กœ ๊ณต๋ถ€ํ•˜๋Š” ๋ฒ•
    • ๋ฐ”๋‹๋ผ์ฝ”๋”ฉ ์ˆ˜๊ฐ• ํ›„๊ธฐ
    • async ๊ณผ์ œ ํ›„๊ธฐ - ๋น„๋™๊ธฐ, ๋™๊ธฐ, ํด๋กœ์ €, ๋ฐฐ์—ด๊ณผ ๊ฐ์ฒด
    • ์œ ์šฉํ•œ ์‚ฌ์ดํŠธ
  • algorithm
    • The Supermarket Queue
    • Find the odd int
    • The Office III - Broken Photocopier
    • Directions Reduction
    • The Office II - Boredom Score
    • Divisible Sum Pairs
    • Codewars ์ด์šฉ์ž ์†”๋ฃจ์…˜ ๋ชจ์Œ
    • Shortest Word
    • find key
    • Two Sum
    • Simple Pig Latin
  • Book
    • the essence of object-orientation
      • ํƒ€์ž…๊ณผ ์ถ”์ƒํ™”
      • ๊ฐ์ฒด ์ง€๋„
      • ์ด์ƒํ•œ ๋‚˜๋ผ์˜ ๊ฐ์ฒด
      • ์ถ”์ƒํ™” ๊ธฐ๋ฒ•
      • 05. ์ฑ…์ž„๊ณผ ๋ฉ”์‹œ์ง€
      • 07.ํ•จ๊ป˜ ๋ชจ์œผ๊ธฐ
      • 04. ์—ญํ• , ์ฑ…์ž„, ํ˜‘๋ ฅ
      • ํ˜‘๋ ฅํ•˜๋Š” ๊ฐ์ฒด๋“ค์˜ ๊ณต๋™์ฒด
  • ecma-script2015
    • Object Literal Upgrades
    • default-parameter-template-literals-arrow-functions
    • spread-operator-rest-param
    • let-const-rest-parameter-spread-operator-destructuring
  • http
    • ์›น ๋ธŒ๋ผ์šฐ์ €์— URL์„ ์ž…๋ ฅํ–ˆ์„ ๋•Œ ์–ด๋–ป๊ฒŒ ์›น ํŽ˜์ด์ง€๊ฐ€ ๋ณด์—ฌ์งˆ๊นŒ?
  • jest
    • toThrow(error?)
  • data-structures
    • Data Structures
  • express
    • express generator
    • CORS
  • css-flexible-box-layout
    • flex ํ•ด๋ฒ„๋ ธ์ง€ ๋ญ์•ผ
  • git
    • Git
  • mongodb
    • MongoDB
  • markdown
    • use-markdown
  • cmder
    • cmd ๋ช…๋ น์–ด ๋ชจ์Œ
  • debug
    • trackClicksEx function error
  • npm
    • NPM TOKEN ์„ค์ •ํ•˜๊ธฐ
  • storybook
    • Storybook
  • sort
    • Sorting Algorithms - part 1
  • javascript-koans
    • Javascript Koans ์˜ค๋‹ต๋…ธํŠธ
  • rxjs
    • Rx.js
  • dom-elements
    • HTML Element
  • redux-toolkit
    • Redux Toolkit
  • github-actions
    • GitHub Actions
  • redux-middleware
    • redux middleware
  • rest
    • rest
  • css-rendering
    • ์ฝ”๋“œ ์Šคํ”ผ์ธ  - CSS Rendering 1ํšŒ์ฐจ 2/2
    • ์ฝ”๋“œ ์Šคํ”ผ์ธ  - CSS Rendering 1ํšŒ์ฐจ 1/2
  • you-dont-know-js
    • ํƒ€์ž…
  • server
    • # shutdown local server
  • semantic-versioning
    • Semantic Versioning 2.0.0
Powered by GitBook
On this page
  • Syntax
  • Parameters
  • Return value
  • Examples
  • Regexp.exec() ์™€ matchAll()
  • ์บก์ณ ๊ทธ๋ฃน์— ๋” ์—‘์„ธ์Šค ํ–ฅ์ƒ (String.prototype.match() ๋ณด๋‹ค)
  • Browser compatibility

Was this helpful?

  1. javascript-api

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

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์—์„œ ์ง€์›์ด ์•ˆ๋œ๋‹ค.

PreviousString.rawNextstring.repeat

Last updated 5 years ago

Was this helpful?

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

iterator