• JavaScript Video Tutorials

JavaScript - Symbol.match Property



A JavaScript symbol called Symbol.match is used as a key to the method that determines an object's behavior in a regular expression matching operation. It allows you to customize how objects match, for example, how they react to calls to the String.prototype.match() method.

Using this symbol, programmers can construct custom matching logic for objects that goes beyond the built-in JavaScript types behavior. Developers can use this feature to expand the regular expression matching's adaptability to a larger variety of data kinds and structures in their applications.

Syntax

Following is the syntax of JavaScript Symbol.match property −

regexp[Symbol.match] = false;

Parameters

This property doesn't accept any kind of parameters

Return value

This returns the Boolean value for the string matching. If match is found it returns true otherwise false.

Examples

Example 1

Let's look at the following example, where we are going to customize the matching behaviour.

<html>
   <style>
      body {
         font-family: verdana;
         color: #DE3163;
      }
   </style>
   <body>
      <script>
         const x = {
            [Symbol.match](a) {
               return a.indexOf('Tp') !== -1 ? 'true' : false;
            }
         };
         document.write('TutorialsPoint Tp'.match(x));
      </script>
   </body>
</html>

If we execute the above program, it will displays the text on the webpage.

Example 2

Consider the another scenario, where we are going to use the Symbol.match to define matching behaviour for a object.

<html>
   <style>
      body {
         font-family: verdana;
         color: #DE3163;
      }
   </style>
   <body>
      <script>
         let x = {
            [Symbol.match](a) {
               return 'TutorialsPoint'.indexOf(a) !== -1;
            }
         };
         document.write('M'.match(x), " < br > ");
         document.write('P'.match(x));
      </script>
   </body>
</html>

On executing the above script, it will displays a text on the webpage.

Example 3

In the following example, we are going to use Symbol.match property to match only alphabets.

<html>
   <style>
      body {
         font-family: verdana;
         color: #DE3163;
      }
   </style>
   <body>
      <script>
         const x = {
            [Symbol.match]: function(string) {
               return string.match(/[a-zA-Z]+/g);
            }
         };
         const a = 'T123U456T6272O673R262I522X'.match(x);
         document.write(a);
      </script>
   </body>
</html>

When we execute the script, it will displays a text on the webpage.

Example 4

Following is the example, where we are going to use Symbol.match to match specific pattern.

<html>
   <style>
      body {
         font-family: verdana;
         color: #DE3163;
      }
   </style>
   <body>
      <script>
         const x = {
            [Symbol.match]: function(string) {
               return string.match(/(\d+)/g);
            }
         };
         const a = 'T1U2T3O1R4I3X'.match(x);
         document.write(a);
      </script>
   </body>
</html>

On executing the above script, the output window will pop up, displaying the numbers on the webpage.

Advertisements