Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Symbol.match

BaselineWidely available

TheSymbol.match static data property represents thewell-known symbolSymbol.match. TheString.prototype.match() method looks up this symbol on its first argument for the method used to match an input string against the current object. This symbol is also used to determine if an object should betreated as a regex.

For more information, seeRegExp.prototype[Symbol.match]() andString.prototype.match().

Try it

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

Value

The well-known symbolSymbol.match.

Property attributes ofSymbol.match
Writableno
Enumerableno
Configurableno

Description

This function is also used to identifyif objects have the behavior of regular expressions. For example, the methodsString.prototype.startsWith(),String.prototype.endsWith() andString.prototype.includes(), check if their first argument is a regular expression and will throw aTypeError if they are. Now, if thematch symbol is set tofalse (or aFalsy value exceptundefined), it indicates that the object is not intended to be used as a regular expression object.

Examples

Marking a RegExp as not a regex

The following code will throw aTypeError:

js
"/bar/".startsWith(/bar/);// Throws TypeError, as /bar/ is a regular expression// and Symbol.match is not modified.

However, if you setSymbol.match tofalse, the object will be considered asnot a regular expression object. The methodsstartsWith andendsWith won't throw aTypeError as a consequence.

js
const re = /foo/;re[Symbol.match] = false;"/foo/".startsWith(re); // true"/baz/".endsWith(re); // false

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-symbol.match

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp