Symbol.match
BaselineWidely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since January 2020.
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 | |
---|---|
Writable | no |
Enumerable | no |
Configurable | no |
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
:
"/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.
const re = /foo/;re[Symbol.match] = false;"/foo/".startsWith(re); // true"/baz/".endsWith(re); // false
Specifications
Specification |
---|
ECMAScript® 2026 Language Specification # sec-symbol.match |