Last active
July 13, 2017 19:47
-
-
Save belchior/7c32b03f48b75948a896a8bc2581f0fa to your computer and use it in GitHub Desktop.
Convert String with regex pattern to RegExp object
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
function toRegExp(text) { | |
var flags; | |
var reg; | |
text = typeof text === 'string' ? text : ''; | |
flags = text.replace(/.*\/([igmy]*)$/, '$1'); | |
reg = text.replace(/^\/(.*)\/[igmy]*$/, '$1'); | |
return new RegExp(reg, flags); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
usage:
toRegExp('/^http:/gi')
toRegExp('/(a|b)+[c]*/g')