Wednesday, September 25, 2024
Google search engine
HomeLanguagesJavascriptJavaScript Symbol split Property

JavaScript Symbol split Property

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('neveropen'.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('neveropen'.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.

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments