• JavaScript Video Tutorials

JavaScript - Symbol.split Property



The Symbol.split property is a symbol value that serves as the key for a well-known symbol in JavaScript. Similar to the built-in String.prototype.split() method, it is used as a method to split a string object into an array of substrings based on a provided separator.

Usually, when a string is split using the String.prototype.split() function with a regular expression as the separator, the split method is called internally. This enables programmers to alter the splitting behavior of stings.

Syntax

Following is the syntax of JavaScript Symbol.split property −

[Symbol.split](string)

Parameters

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

Return value

This property returns a string that is split from the specified expression.

Examples

Example 1

Let's look at the following example, where we are going to split the vowels.

<html>
   <style>
      body {
         font-family: verdana;
         color: #DE3163;
      }
   </style>
   <body>
      <script>
         const x = {
            [Symbol.split](str) {
               return str.split(/[AEIOU]/);
            }
         };
         const a = "TUTORIALSPOINT";
         document.write(a.split(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 split on the specific word.

<html>
   <style>
      body {
         font-family: verdana;
         color: #DE3163;
      }
   </style>
   <body>
      <script>
         const x = {
            [Symbol.split](str) {
               const y = 'TUTORIX';
               const z = str.indexOf(y);
               return [str.substring(0, z), str.substring(z)];
            }
         };
         const a = "TUTORIALSPOINTTUTORIX";
         document.write(a.split(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 split the string into characters.

<html>
   <style>
      body {
         font-family: verdana;
         color: #DE3163;
      }
   </style>
   <body>
      <script>
         const a = "WELCOME";
         const x = {
            [Symbol.split](string) {
               return string.split("");
            }
         };
         document.write(a.split(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 split.

<html>
   <style>
      body {
         font-family: verdana;
         color: #DE3163;
      }
   </style>
   <body>
      <script>
         const x = {
            [Symbol.split](str) {
               return str.split(/[,.\s]+/);
            }
         };
         const a = "Welcome,To.EveryOne";
         const y = a.split(x);
         document.write(y);
      </script>
   </body>
</html>

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

Advertisements