becouse /.../
are too dense, and new RegExp(...)
too messy
In examples below, _
is the lodash
library.
re.i`\b${myName}\b`.test('My name is Robert.')
is equivelant to
new RegExp(`\\b${_.escapeRegExp(myName)}\\b`, 'i').test('My name is Robert.')
text.replace(re.gi`\b(?:${badWords})\b`, 'BIP')
is equivelant to
const reBadWords = new RegExp(`\\b(?:${badWords.map(_.escapeRegExp).join('|')})\\b`, 'gi')
text.replace(reBadWords, 'BIP')
Named groups are great, but not too practical if limited to one line with no spaces!
re
ignores (removes) whitespaces except those that you explicitly escape (\
).
const reISODate = re`
(?<date>
(?<YYYY> \d{4})
-
(?<MM> \d{2})
-
(?<DD> \d{2}) ${[/* Here a comment if you need one */]}
)
(?:
(?: T | \ )
(?<time>
(?<HH> \d{2})
:
(?<mm> \d{2})
:
(?<ss> \d{2})
(?:
\.
(?<ms> \d{3})
)?
)
(?:
(?<zone> Z) | (?<offset> [+-]\d\d(?: :\d\d )?)
)?
)?
`
const reJustISODate = re`^ ${reISODate} $`
console.log(new Date().toISOString().match(reJustISODate).groups)