const isValidRegExpCache = { } ; const safelyCreateRegExpCache = { } ; export function serializeRegStr ( str ) { const chars = '\\{}[]+$^/!.*|():?,=<>' ; Array . prototype . forEach . call ( chars , ( char ) => { str = str . replace ( new RegExp ( '\\' + char , 'gi' ) , '\\' + char ) ; } ) ; return str ; } export function isRegStrValid ( strRegExp , parameters = 'gi' ) { if ( typeof strRegExp !== 'string' ) { return false ; } const key = `__${ strRegExp }__` ; if ( isValidRegExpCache [ key ] !== void 0 ) { return isValidRegExpCache [ key ] ; } try { const regExp = new RegExp ( strRegExp , parameters ) ; if ( regExp instanceof RegExp ) { isValidRegExpCache [ key ] = true ; } } catch ( error ) { isValidRegExpCache [ key ] = false ; } return isValidRegExpCache [ key ] ; } export function createFromStr ( strRegExp , parameters = 'gi' ) { if ( typeof strRegExp !== 'string' ) { return new Error ( `Invalid regular expresion. String should be as source.` ) ; } const key = `__${ strRegExp }__${ parameters }__` ; if ( safelyCreateRegExpCache [ key ] !== void 0 ) { return safelyCreateRegExpCache [ key ] ; } if ( isRegStrValid ( strRegExp , parameters ) ) { strRegExp = strRegExp . replace ( /\\/gi , '\\' ) ; safelyCreateRegExpCache [ key ] = new RegExp ( strRegExp , parameters ) ; } else if ( isRegStrValid ( serializeRegStr ( strRegExp ) , parameters ) ) { safelyCreateRegExpCache [ key ] = new RegExp ( serializeRegStr ( strRegExp ) , parameters ) ; } else { safelyCreateRegExpCache [ key ] = new Error ( `Invalid regular expresion` ) ; } return safelyCreateRegExpCache [ key ] ; } export function createFromSerializedStr ( strRegExp , parameters = 'gi' ) { if ( typeof strRegExp !== 'string' ) { return new Error ( `Invalid regular expresion. String should be as source.` ) ; } strRegExp = strRegExp . replace ( /</gi , '&lt;' ) . replace ( />/gi , '&gt;' ) ; return createFromStr ( strRegExp , parameters ) ; }