Created
September 19, 2019 23:13
-
-
Save luisenriquecorona/d8f414c538c504d6a4dbbca0a27a12c1 to your computer and use it in GitHub Desktop.
The Symbol.match, Symbol.replace, Symbol.search, and Symbol.split Properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// effectively equivalent to /^.{10}$/ | |
let hasLengthOf10 = { | |
[Symbol.match]: function(value) { | |
return value.length === 10 ? [value.substring(0, 10)] : null; | |
}, | |
[Symbol.replace]: function(value, replacement) { | |
return value.length === 10 ? replacement + value.substring(10) : value; | |
}, | |
[Symbol.search]: function(value) { | |
return value.length === 10 ? 0 : -1; | |
}, | |
[Symbol.split]: function(value) { | |
return value.length === 10 ? ["", ""] : [value]; | |
} | |
}; | |
let message1 = "Hello world", // 11 characters | |
message2 = "Hello John"; // 10 characters | |
let match1 = message1.match(hasLengthOf10), | |
match2 = message2.match(hasLengthOf10); | |
console.log(match1); // null | |
console.log(match2); // ["Hello John"] | |
let replace1 = message1.replace(hasLengthOf10), | |
replace2 = message2.replace(hasLengthOf10); | |
console.log(replace1); // "Hello world" | |
console.log(replace2); // "Hello John" | |
let search1 = message1.search(hasLengthOf10), | |
search2 = message2.search(hasLengthOf10); | |
console.log(search1); // -1 | |
console.log(search2); // 0 | |
let split1 = message1.split(hasLengthOf10), | |
split2 = message2.split(hasLengthOf10); | |
console.log(split1); // ["Hello world"] | |
console.log(split2); // ["", ""] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment