Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 2x 2x 5x 5x 4x 4x 1x 1x 1x 1x | /** * Check whether element with index 1 exists in array */ const checkForExistenceOfArrayElementOne = (array) => (array && array[1]); /** * Create a regular expression from string using JS new RegExp * @param {String} string - string representation of regex * @returns {Regexp} - Regular expression derived from string */ const createRegExpFromString = (string) => { const matchedFlags = string.match(/.*\/([gimy]*)$/); let flags = ''; if(checkForExistenceOfArrayElementOne(matchedFlags)) { flags = matchedFlags[1]; } const matchedPattern = string.match(new RegExp('^/(.*?)/'+flags+'$')); if(checkForExistenceOfArrayElementOne(matchedPattern)) { return new RegExp(matchedPattern[1], flags); } throw new Error('Invalid regular expression, missing / at beginning and beteen pattern and flags, or flags are invalid. (Don\'t forget to escape special chars.)'); } module.exports = createRegExpFromString; |