rexrex
🐺 Regular Expression utils that rock!
Create regular expressions that are composable, reusable, and commentable.
Getting started
yarn add rexrex
Utils
whole
// -> ^sentence to match$
repeat
// -> \\d // -> \\d{8} // -> \\d{1,3} // -> \\d{1,}
numeric
Equivalent to rex.repeat.bind(null, '\\d')
alpha
Equivalent to rex.repeat.bind(null, '[A-z]')
and
// -> 'abc'
or
// -> 'a|b|c'
wildcard
and extra
// -> '.*' // -> '.*?' // -> '.+?' // -> '.+'
capture
// -> (\\d+?)
or you can name your capture group with capture(pattern, name)
// -> (?<number>\\d+?)
group
Similar to a capture(...), but won't keep the capture within the parentheses
// -> (?:.|\\s)
look.(ahead|behind).(positive|negative)
Creates a negative or positive look-ahead
lookahead === look // -> '(?=y)'lookahead // -> '(?!y)'lookbehind === look // -> '(?<=y)'lookbehind // -> '(?<!y)'
regex
Equal to RegExp
constructor
Matchers
ALPHA
:'[A-z]'
WORD
:'\\w'
NUMBER
:'\\d'
WHITE_SPACE
:'\\s'
ANY
:'.'
START
:'^'
END
:'$'
LAZY
:'?'
not
Matches opposite of matchers
// -> '[^A-z]'
Flags
GLOBAL
:'g'
MULTI_LINE
:'m'
INSENSITIVE
:'i'
STICKY
:'y'
UNICODE
:'u'
Examples
See index.spec.js
for all the uses!
// found in `graphql-types-drivers-license` // Matches all New York Driver's licenses// -> /[A-z]{1}\d{7}|[A-z]{1}\d{18}|\d{8,9}|\d{16}|[A-z]{8}/
// matches GraphQL queries/mutations // -> /((fragment|query|mutation|subscription)\s+\w+\s+(on\s+\w+)*)\s*(({(\s|\w|(".*")|:|,|\.|\)|\()+)+(}(\s|\w|(".*")|:|,|\.|\)|\()+)+)+}/g
Bonus
- Tiny!
- Super-readable!
- Changes make sense!