2018-05-02 09:45:26 -07:00
|
|
|
/**
|
|
|
|
* Constant value for an empty regex source string
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
const emptyRegexSource = '(?:)';
|
|
|
|
|
2018-04-16 10:30:26 -07:00
|
|
|
/**
|
|
|
|
* Sanitizes a string to be used in creating a runtime regular expression pattern by escaping special characters
|
|
|
|
* @param {string} pattern the string intended to be used to new a RegExp object
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
const sanitizeRegExp = (pattern: string): string => pattern.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new RegExp object using the pattern argument
|
|
|
|
* @param {string} pattern the string to build the regular expression with
|
|
|
|
* @param {string} [flags] flags to be passed to the RegExp constructor
|
|
|
|
* @returns {RegExp}
|
|
|
|
*/
|
|
|
|
const buildSaneRegExp = (pattern: string, flags?: string): RegExp => new RegExp(sanitizeRegExp(pattern), flags);
|
|
|
|
|
|
|
|
export default buildSaneRegExp;
|
|
|
|
|
2018-05-02 09:45:26 -07:00
|
|
|
export { sanitizeRegExp, buildSaneRegExp, emptyRegexSource };
|