• JavaScript Video Tutorials

JavaScript - Symbol.search Property



The Symbol.search property in JavaScript is a special symbol value that serves as the key for a well-known symbol. This symbol is used as a property key on objects to define custom behavior for searching within strings. It's primarily used in conjunction with regular expressions and the String.prototype.search() method.

When the Symbol.search property is used as a method on an object, it defines how the object behaves when its search() method is called. By default, the search() method searches for a specified value in the string and returns the index of the first occurrence, or -1 if the value cannot be found.

Syntax

Following is the syntax of JavaScript Symbol.search property −

[Symbol.search](string)

Parameters

This property accepts only one parameter i.e, String.

Return value

This property returns the position of string where it matches orelse it will return '-1' if no match found.

Examples

Example 1

Let's look at the following example, where we are going to create a custom object with a method that utilizes the Symbol.search

<html>
   <style>
      body {
         font-family: verdana;
         color: #DE3163;
      }
   </style>
   <body>
      <script>
         const x = {
            [Symbol.search]: function(string, searchValue) {
               return string.indexOf(searchValue);
            }
         };
         const a = "Welcome, TutorialsPoint";
         document.write(x[Symbol.search](a, "TutorialsPoint"));
      </script>
   </body>
</html>

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

Example 2

Consider the another scenario, where we are going to use the Symbol.search with a custom class.

<html>
   <style>
      body {
         font-family: verdana;
         color: #DE3163;
      }
   </style>
   <body>
      <script>
         class x {
            constructor(value) {
               this.value = value;
            }
            [Symbol.search](string) {
               return string.indexOf(this.value);
            }
         }
         const a = new x('EveryOne');
         document.write('Welcome EveryOne'.search(a));
      </script>
   </body>
</html>

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

Example 3

In the following example, we are going to return the '-1' if no match was found.

<html>
   <style>
      body {
         font-family: verdana;
         color: #DE3163;
      }
   </style>
   <body>
      <script>
         const a = "   Welcome To The TutorialsPoint";
         const x = /is/;
         Symbol.search = function(string, pattern) {
            const index = string.indexOf(x);
            return index === -1 ? -1 : index;
         };
         document.write(a.search(x));
      </script>
   </body>
</html>

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

Example 4

Following is the example, where we are going to take the regular expression and perform the matching.

<html>
   <style>
      body {
         font-family: verdana;
         color: #DE3163;
      }
   </style>
   <body>
      <script>
         const x = {
            [Symbol.search](string) {
               const regex = /The/;
               return regex.exec(string).index;
            }
         };
         const str = "Welcome To The World";
         document.write(str.search(x));
      </script>
   </body>
</html>

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

Advertisements