Skip to content

Instantly share code, notes, and snippets.

@ephys
Created September 14, 2016 13:31
Show Gist options
  • Save ephys/2c24ffe761eba929ec643e7f393e3799 to your computer and use it in GitHub Desktop.
Save ephys/2c24ffe761eba929ec643e7f393e3799 to your computer and use it in GitHub Desktop.
// 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