Created
September 14, 2016 13:31
-
-
Save ephys/2c24ffe761eba929ec643e7f393e3799 to your computer and use it in GitHub Desktop.
This file contains 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
// new RegExp('/test/i') | |
// is interpreted as /\/test\/i/ | |
// this function interprets it as | |
// /test/i | |
function parseRegExp(regExpStr) { | |
if (typeof regExpStr !== 'string') { | |
throw new TypeError('Invalid argument: not a string.'); | |
} | |
const start = regExpStr.indexOf('/'); | |
const end = regExpStr.lastIndexOf('/'); | |
if (start === -1 || start === end) { | |
throw new TypeError('Invalid RegExp syntax, matcher should be surrounded with slashes.'); | |
} | |
const matcher = regExpStr.substring(start + 1, end); | |
if (matcher === '') { | |
throw new TypeError('Invalid RegExp syntax, empty matcher.'); | |
} | |
const options = regExpStr.substr(end + 1); | |
return new RegExp(matcher, options); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment