Open In App

JavaScript Symbol match Property

JavaScript Symbol match property is used to identify the matching of a regular expression against a string and this function is called using String match() method.

Syntax:



regexp[Symbol.match] = false;

Parameters: It does not accept any parameters. 

Return value: It will return the Boolean value for a string matching if matches found then it will return true otherwise returns false. Below examples illustrate the Symbol.match property in JavaScript.



Below examples illustrate the JavaScript Symbol match Property:

Example 1: In this example, we will check if a string starts with a particular expression defined in regexp




const regexp1 = /geeksforgeeks/;
  
regexp1[Symbol.match] = false;
  
console.log('/geeks/'.startsWith(regexp1));
console.log('/geeksforgeeks/'.endsWith(regexp1));

Output:

false
true

Example 2: In this example, we will pass a regular expression and see the type of error returned. 




reg[Symbol.match] = false;  
  
console.log('/bar/'.startsWith(/bar/));

Output:

Error: First argument to String.prototype.startsWith must not be a regular expression.

Supported Browsers: The browsers supported by Symbol.match property are listed below:

We have a complete list of Javascript symbols’ properties and methods, to check those please go through the Javascript Symbol Complete Reference article.

Article Tags :