123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- const RE_REGEXP_CHAR = /[\\^$.*+?()[\]{}|]/gu
- const RE_HAS_REGEXP_CHAR = new RegExp(RE_REGEXP_CHAR.source)
- const RE_REGEXP_STR = /^\/(.+)\/(.*)$/u
- function escape(string) {
- return string && RE_HAS_REGEXP_CHAR.test(string)
- ? string.replace(RE_REGEXP_CHAR, '\\$&')
- : string
- }
- function toRegExp(string) {
- const parts = RE_REGEXP_STR.exec(string)
- if (parts) {
- return new RegExp(parts[1], parts[2])
- }
- return new RegExp(`^${escape(string)}$`)
- }
- function isRegExp(string) {
- return Boolean(RE_REGEXP_STR.exec(string))
- }
- module.exports = {
- escape,
- toRegExp,
- isRegExp
- }
|