Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

JavaScript Symbol split Property

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

JavaScript Symbol split property is used to specify the method that splits a string at the indices that match a regular expression. This property is called by the String.split() method.

Syntax:

[Symbol.split](string)

Property attributes: It accepts a “String” which is not Writable, Enumerable, and Configurable.

Return value: It returns a string that is split from the given expression.

Below examples illustrate the Symbol split property in JavaScript:

Example 1: In this example, we will split the string and add (“”) in it.

Javascript




class Split1 {
    constructor(value) {
            this.value = value;
        }
        [Symbol.split](string) {
            const index = string.indexOf(this.value);
            return "'" + string.substr(0, index) + "' '" +
                this.value + "' '" + string.substr(index + this.value.length) + "'";
        }
}
  
console.log('GeeksforGeeks'.split(new Split1('for')));
console.log('Geeks1Geeks2Geeks3Geeks4'.split(new Split1('Geeks')));

Output:

"'Geeks' 'for' 'Geeks'"
"'' 'Geeks' '1Geeks2Geeks3Geeks4'"

Example 2: In this example, we will split the string and add (_) in it.

Javascript




class Split1 {
    constructor(value) {
            this.value = value;
        }
        [Symbol.split](string) {
            const index = string.indexOf(this.value);
            return "_" + string.substr(0, index) + "__" +
                this.value + "__" + string.substr(index + this.value.length) + "_";
        }
}
  
console.log('GeeksforGeeks'.split(new Split1('for')));
console.log('Computer Science Portal'.split(new Split1(' ')));

Output:

_Geeks__for__Geeks_
_Computer__ __Science Portal_

Supported Browsers: The browsers supported by JavaScript Symbol split property are listed below:

  • Google Chrome 51
  • Firefox 50
  • Edge 15
  • Opera
  • Apple Safari

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


My Personal Notes arrow_drop_up
Last Updated : 19 May, 2023
Like Article
Save Article
Similar Reads
Related Tutorials