mauss
Version:
practical functions and reusable configurations
11 lines (10 loc) • 488 B
JavaScript
/**
* A drop-in replacement for `new RegExp()` with special characters from source string escaped. This is useful when the pattern is not known at compile time and is dynamically constructed.
*
* @param pattern passed in the form of string literal
* @param flags unique set of characters from `d|g|i|m|s|u|y`
* @returns dynamically constructed RegExp object
*/
export function regexp(pattern, flags) {
return new RegExp(pattern.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), flags);
}