Builds composable regular expression, which compiles to native RegExp. And generates a matching string for it.
// [ 'foo', 'bar' ]
'foo bar'.match( word )
// [ 'foo', 'bar' ]
for( const token of 'foo bar'.matchAll( word ) ) {
// Full matched substring
token[0]
// Catched subgroups by names like `{ foo: 'bar', ... }`
// `undefined` when token doesn't matched.
token.groups
}
// "foo@example.org"
mail.generate({
dot_atom: 'foo',
domain: 'example.org',
})
// Error: "Wrong param: dot_atom=jin."
mail.generate({
dot_atom: 'jin.',
domain: 'example.org',
})
Default Value from Pattern
// "jin: male"
sexism.generate({
name: 'jin',
male: true,
})
More examples in tests.
// /:\)/gsu
const smile = $mol_regexp.from( ':)' )
// /hello/gimsu
const hello = $mol_regexp.from( 'hello', {
ignoreCase: true,
multiline: true,
} )
// /.../gmsu
const triplet = $mol_regexp.from(
$mol_regexp.from(
/.../,
{ ignoreCase: true },
),
{ multiline: true },
)
// /\u{20}/gsu
const space = $mol_regexp.from( 32 )
// /[,;\u{20}\u{61}-\u{7a}\d]/gsu
const tags = $mol_regexp.char_only(
',;', // chars
32, // code of space
$mol_regexp.char_range( 0x61, 0x7A ), // a-z
$mol_regexp.decimal_only, // 0-9
)
// /[^,;\u{20}\u{61}-\u{7a}\d]/gsu
const tags = $mol_regexp.char_except(
',;', // chars
32, // code of space
$mol_regexp.char_range( 0x61, 0x7A ), // a-z
$mol_regexp.decimal_only, // 0-9
)
// /\p{Script=Cyrillic}/gsu
$mol_regexp.unicode_only( 'Script', 'Cyrillic' )
// /\P{Script=Cyrillic}/gsu
$mol_regexp.unicode_except( 'Script', 'Cyrillic' )
// /\p{Hex_Digit}/gsu
$mol_regexp.unicode_only( 'Hex_Digit' )
// /\P{Hex_Digit}/gsu
$mol_regexp.unicode_except( 'Hex_Digit' )
// Regexp: /(?:\n){1,2}?/gsu
const para_sep = $mol_regexp.repeat( '\n', 1, 2 )
// /(?:\d){2,4}/
const year = $mol_regexp.repeat_greedy( $mol_regexp.decimal_only, 2, 4 )
Sequence and optional sequence
// /(?:\d){2,4}(?:\.\.(?:\d){2,4}){0,1}/gsu
const life_years = $mol_regexp.from([ year, [ '..', year ] ])
const from = year
const to = year
// /(?:((?:\d){2,4}))(?:\.\.(?:((?:\d){2,4}))){0,1}/gsu
const life_years = $mol_regexp.from([ {from}, [ '..', {to} ] ])
Matches |
from |
to |
95..99 |
95 |
99 |
2020 |
2020 |
|
enum Sex {
male = 'male',
female = 'female',
}
// /(?:((?:(male)|(female))))/gsu
const sex = $mol_regexp.from( {Sex} )
// { Sex: string, male: string, female: string } | undefined
const res = [ ... text.matchAll( sex ) ][0].groups
Matches |
Sex |
male |
female |
male |
male |
male |
|
female |
female |
|
female |
const {
begin, end,
char_only, char_range,
latin_only, slash_back,
repeat_greedy, from,
} = $mol_regexp
const atom_char = char_only( latin_only, "!#$%&'*+/=?^`{|}~-" )
const atom = repeat_greedy( atom_char, 1 )
const dot_atom = from([ atom, repeat_greedy([ '.', atom ]) ])
const name_letter = char_only(
char_range( 0x01, 0x08 ),
0x0b, 0x0c,
char_range( 0x0e, 0x1f ),
0x21,
char_range( 0x23, 0x5b ),
char_range( 0x5d, 0x7f ),
)
const quoted_pair = from([
slash_back,
char_only(
char_range( 0x01, 0x09 ),
0x0b, 0x0c,
char_range( 0x0e, 0x7f ),
)
])
const name = repeat_greedy({ name_letter, quoted_pair })
const quoted_name = from([ '"', {name}, '"' ])
const local_part = from({ dot_atom, quoted_name })
const domain = dot_atom
const mail = from([ begin, local_part, '@', {domain}, end ])
Matches |
domain |
dot_atom |
name |
name_letter |
quoted_name |
quoted_pair |
a.b@c.d |
c.d |
a.b |
|
|
|
|
"a\n"@c.d |
c.d |
|
a\n |
|
"a\n" |
\n |
npm install mol_regexp

import { $mol_regexp } from 'mol_regexp'
Sandbox