regexmoji
v1.0.0
Published
A tiny utility for writing easy to read regexps
Downloads
1
Maintainers
Readme
regexmoji
regexmoji is an experimental module. Its main goal is to make regular expressions easier to read and maintain. Just like how in CoffeeScript, annotating regular expressions is possible (and I love that), with this plugin you can do this in vanilla JS. Also, a key advantage over CoffeeScript is that, it can also reduce repetition.
Usage
This is one of the most confusing email validation patterns i can find online:
/[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/g
With regexmoji, it can reduce to:
import regexmoji from 'regexmoji'
regexmoji(
/💜(?:\.💜)*@(?:💚(?:💧💚)?\.)+💚(?:💧💚)?/g,
{
"💚": /[a-z0-9]/, // alphanumeric (single)
"💧": /[a-z0-9-]*/, // alphanumeric and dash (multi)
"💜": /[a-z0-9!#$%&'*+/=?^_`{|}~-]+/, // stuff
}
);
It became easier to read, and even entertaining! This is just like a RegExp equivalent of template literals. I wish they existed. However, even if they existed, they would look something like this:
RegExp`/${C}(?:\.${C})*@(?:${A}(?:${B}${A})?\.)+${A}(?:${B}${A})?/g`
So I like this emoji approach better. Especially visually.
API
Basic usage:
import regexmoji from 'regexmoji'
let regex = regexmoji(inputRegex, dictionary)
Shorthand:
import {rm} from 'regexmoji'
let regex = rm(inputRegex, dictionary)
Custom, re-usable:
import {create} from 'regexmoji'
let rm = create(dictionary)
let regex1 = rm(inputRegex1)
let regex2 = rm(inputRegex2)
See Also
- https://github.com/rauschma/re-template-tag
- http://xregexp.com
- https://github.com/shannonmoeller/regx