The Symbol.split property in JavaScript is a well-known symbol which is used to specify the method that splits a string at the indices that match a regular expression. This function is called by the String.prototype.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 split from the given expression.
Below examples illustrate the Symbol.split property in JavaScript:
Example 1:
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:
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)+ "_" ; } } document.write( 'GeeksforGeeks' .split( new Split1( 'for' ))); document.write( "<br>" ); document.write( 'Computer Science Portal' .split( new Split1( ' ' ))); |
Output:
_Geeks__for__Geeks_ _Computer__ __Science Portal_
Supported Browsers: The browsers supported by JavaScript Symbol.split properties are listed below:
- Google Chrome 51
- Firefox 50
- Edge 15
- Opera
- Apple Safari
Reference: https://devdocs.io/javascript/global_objects/symbol/split