I liked this gist enough that I made it a full-blown npm package. Check it out here.
After having used regular expression escape functions for years, it just crossed my mind that this might be way easier to achieve with a tagged template literal.
This is how to use this gist:
import rx from 'https://unpkg.com/@loilo/rx/dist/rx.mjs'
const disallowedWindowsFilenameChars = rx`[${'<>:"/\\|?*'}]`
if (disallowedWindowsFilenameChars.test(someFilename)) {
console.error('Invalid characters in filename')
} else {
console.log("You're probably fine")
}
Disclaimer: Please keep in mind that all code examples in this gist are exclusively for demonstrational purposes. Most of them can be solved more efficiently and elegantly without any use of regular expressions.
To use flags in our regular expression, we can pass them to the rx()
function and use the result as a template:
function matchCaseInsensitive(string) {
return rx('i')`${string}`
}
const pattern = matchCaseInsensitive('foo')
pattern.test('foo') // true
pattern.test('fOO') // true
From time to time, we may want to include control characters in some kind of conditional when rx
inadvertently escapes them:
function naiveNumberMatcher(allowFloat) {
return rx`^-?[0-9]+${allowFloat ? '(\\.[0-9]+)?' : ''}$`
}
const pattern = naiveNumberMatcher(true)
// pattern = /^-?[0-9]+\(\\\.\[0\-9\]\+\)\?$/
// Snap! This won't match floating point numbers.
Luckily, there's an easy solution: just return the control characters as a regular expression:
function naiveNumberMatcher(allowFloat) {
return rx`^-?[0-9]+${allowFloat ? /(\.[0-9]+)?/ : ''}$`
}
const intPattern = naiveNumberMatcher(false) // intPattern = /^-?[0-9]+$/
intPattern.test('abc') // false
intPattern.test('0') // true
intPattern.test('-1') // true
intPattern.test('1.5') // false
const floatPattern = naiveNumberMatcher(true) // floatPattern = /^-?[0-9]+(\.[0-9]+)?$/
floatPattern.test('abc') // false
floatPattern.test('0') // true
floatPattern.test('-1') // true
floatPattern.test('1.5') // true
Alternatively, we could have wrapped the control characters in an rx.raw()
call which will exclude them from being escaped:
function naiveNumberMatcher(allowFloat) {
// rx.raw also works as a template tag: rx.raw`(\\.[0-9]+)?`
return rx`^-?[0-9]+${allowFloat ? rx.raw('(\\.[0-9]+)?') : ''}$`
}
This can be necessary when the wrapped control characters are quantifiers which cannot form a regular expression of their own, e.g. /?/
.
If an array is passed as a placeholder, its entries will be escaped and joined by a vertical bar — this way, we can easily express enumerations:
function oneOf(...strings) {
return rx('i')`^${strings}$`
}
const pattern = oneOf('a', 'b') // pattern = /^a|b$/i
pattern.test('a') // true
pattern.test('B') // true
pattern.test('d') // false
Note that arrays may also contain regular expressions or rx.raw
strings which stay unescaped as described above:
function oneOfTheseOrInteger(...strings) {
return rx('i')`^(${[...strings, /[0-9]+/]})$`
}
const pattern = oneOfTheseOrInteger('a', 'b') // pattern = /^(a|b|[0-9]+)$/i
pattern.test('A') // true
pattern.test('d') // false
pattern.test('42') // true
Arrays can even be nested and are flattened automatically:
const naivePluralize = value => value + 's'
function oneOrMultipleOf(...strings) {
return rx`^${strings.map(string => [string, naivePluralize(string)])}$`
}
oneOrMultipleOf('cat', 'dog') // /^cat|cats|dog|dogs$/i
In the world of programming, you're basically never the first person to come up with a clever trick. I googled my idea and it turned out that Lea Verou published the very same thing in 2018 — and this gist is loosely based on her implementation.
Key differences are that I added type hints (therefore this gist is written in TypeScript) and the aforementioned capability to merge in arrays and raw strings.