compose-regexp.js
Build and compose maintainable regular expressions in JavaScript.
Regular expressions don't do justice to regular grammars.
This makes complex RegExps hard to read, debug and modify...
compose-regexp
to the rescue!
It doesn't make regular grammars more powerful, they are still fundamentally limited, but since they are ubiquitous, we may as well have better tooling to implement them...
Usage
$ npm install --save compose-regexp
import {
sequence, either, capture,
ref, suffix, flags, avoid
} from "compose-regexp";
const string = either(
sequence(
'"',
suffix('*?',
either(
/\\[\s\S]/,
/./
)
),
'"'
),
sequence(
"'",
suffix('*?',
either(
/\\[\s\S]/,
/./
)
),
"'"
),
sequence(
"`",
suffix('*?',
either(
/\\[\s\S]/,
/[\s\S]/
)
),
"`"
)
)
const multiLineComment = sequence(
'/*',
suffix('*?',
/[\s\S]/
),
'*/'
)
const singleLineComment = sequence('//', /[^\n]*\n/)
const comment = either(multiLineComment, singleLineComment)
const matcher = flags('gm',
either(
comment,
string,
capture(either(/[{}]/, '}}'))
)
);
/\/\*[\s\S]*?\*\/|\/\/[^\n]*\n|"(?:\\[\s\S]|.)*?"|'(?:\\[\s\S]|.)*?'|`(?:\\[\s\S]|[\s\S])*?`|([{}]|\}\})/gm
API
General (important) notes:
The regexp
parameters of these functions can be either RegExps or strings.
Special characters in strings are escaped, so that '.*'
is equivalent to /\.\*/
.
Therefore:
> sequence('.', '*').source
'\\.\\*'
The flags of intermediate regexps are ignored, and always reset to false unless set by flags()
.
flags(opts, regexps...)
> flags('gm', /a/)
/a/gm
either(regexps...)
> either(/a/, /b/, /c/)
/a|b|c/
sequence(regexps...)
> sequence(/a/, /b/, /c/)
/abc/
ComposeRegexp inserts non-capturing groups where needed:
> sequence(/a/, /b|c/)
/a(?:b|c)/
lookAhead(regexps...)
> lookAhead(/a/, /b/, /c/)
/(?=abc)/
avoid(regexps...)
Negative look ahead
> avoid(/a/, /b/, /c/)
/(?!abc)/
suffix(operator, regexprs...), greedy(suffix)(regexprs...)
Valid suffixes: (?
, *
, +
, {n}
, {n,}
and {m, n}
; ??
, *?
, +?
, {n}?
, {n,}?
and {m, n}?
)
> suffix("*", either(/a/, /b/, /c/))
/(?:a|b|c)*/
> maybe = suffix('?'); maybe('a')
/a?/
capture (regexprs...)
> capture(/a/, /b/, /c/)
/(abc)/
ref(n)
> ref(1)
/\1/
Caveat emptor, references are absolute. Therefore, refs may be broken if you compose two regexps that use captures.
stringMatcher = sequence(
capture(/['"`]/),
greedy('*',
either(
sequence('\\', ref(1)),
'\\\\',
sequence(avoid(ref(1)), /[\s\S]/)
)
),
ref(1)
)
whooops = sequence(
capture('foo'),
stringMatcher
)
In whoops
, the ref(1)
in stringMatcher actually refers to foo
, not the opening quote.
Fixing this would require an approach far more complex than what I'm doing now (concat the regexps source).
License MIT
The MIT License (MIT)
Copyright (c) 2016 Pierre-Yves Gérardy
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.