JS: RegExp. prototype [Symbol.split]

By Xah Lee. Date: .

what is RegExp.prototype Symbol.split

RegExp.prototype[Symbol.split] is the method that controls how String.prototype.split() behaves when the separator is a regex object.

// these are equivalent

console.log(/-/[Symbol.split]("a-b-c"));
// ["a", "b", "c"]

console.log("a-b-c".split(/-/));
// ["a", "b", "c"]

Example. customized symbol.split

const xRegex = /-/;

// customized split function. always return false
xRegex[Symbol.split] = () => {
 console.log("am called.");
 return false;
};

console.log("a-b-c".split(xRegex));
// am called.
// false