This page was translated from English by the community.Learn more and join the MDN Web Docs community.
Symbol.match
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2020년 1월.
Symbol.match 정적 데이터 속성은잘 알려진 심볼@@match를 나타냅니다.String.prototype.match() 메서드는 입력 문자열을 현재 객체와 일치시키는 데 사용되는 메서드의 첫 번째 인수에서 이 심볼을 조회합니다. 이 심볼은 객체를정규 표현식으로 처리해야 하는지 여부를 결정하는 데도 사용됩니다.
좀 더 많은 정보를 알고 싶으시면RegExp.prototype[@@match]()와String.prototype.match()를 참고하시기 바랍니다.
In this article
시도해 보기
const regexp1 = /foo/;// console.log('/foo/'.startsWith(regexp1));// Expected output (Chrome): Error: First argument to String.prototype.startsWith must not be a regular expression// Expected output (Firefox): Error: Invalid type: first can't be a Regular Expression// Expected output (Safari): Error: Argument to String.prototype.startsWith cannot be a RegExpregexp1[Symbol.match] = false;console.log("/foo/".startsWith(regexp1));// Expected output: trueconsole.log("/baz/".endsWith(regexp1));// Expected output: false값
잘 알려진 심볼@@match.
Property attributes ofSymbol.match | |
|---|---|
| 쓰기 가능 | 불가능 |
| 열거 가능 | 불가능 |
| 설정 가능 | 불가능 |
설명
이 함수는객체에 정규 표현식의 동작이 있는지를 식별하는 데에도 사용됩니다.String.prototype.startsWith(),String.prototype.endsWith() 그리고String.prototype.includes()를 예로 들자면 첫 인수가 정규식인지 확인하고 정규식인 경우TypeError를 발생시킵니다. 이제match 심볼이false(또는undefined을 제외한거짓 같은 값)로 설정되어 있으면 해당 객체를 정규식 객체로 사용할 수 없음을 나타냅니다.
예제
>RegExp가 정규표현식이 아닌 것으로 표시하기
다음 코드는TypeError를 발생시킵니다.
"/bar/".startsWith(/bar/);// /bar/가 정규 표현식이기에 TypeError 발생// 그리고 Symbol.match 는 수정되지 않았습니다.그러나Symbol.match를false로 설정하면 객체는정규식 객체가 아님으로 간주됩니다.startsWith와endsWith 메서드는 결과적으로TypeError를 발생시키지 않습니다.
const re = /foo/;re[Symbol.match] = false;"/foo/".startsWith(re); // true"/baz/".endsWith(re); // false명세서
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-symbol.match> |