Last active
October 25, 2017 16:13
-
-
Save tennisonchan/d705a1becf1f6ff9048cf0bbb72d4b4e to your computer and use it in GitHub Desktop.
Reg Extend: to extend native JS RegExp.
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
| const regexExtract = new RegExp('\/(.*)\/([gimuy]+)?'); | |
| class RegExt extends RegExp { | |
| constructor(input) { | |
| if (typeof input === 'string' || input instanceof String) { | |
| if (regexExtract.test(input)) { | |
| let parts = regexExtract.exec(input); | |
| super(parts[1], parts[2] || ''); | |
| } else { | |
| super(input); | |
| } | |
| } else if (input instanceof RegExp) { | |
| super(input); | |
| } | |
| } | |
| toJSON() { | |
| return this.toString(); | |
| } | |
| } | |
| exports = module.exports = RegExt; |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To extend native JS RegExp on:
JSON.stringify(regEx)which only retruns "{}"new RegExt('/abc/gi') // => /abc/giExamples