Create Regex

Simple and powerful utility to create complex regex in javascript
Create regex
When creating long and powerful regular expression it becomes hard to remember and manage to numerous signs and sub patterns.
create-regex
allows you to write regex in code like approche while also reducing the burden of some boilerplate actions.
It also encourges the usage of regex named groups, which make the matching part of the regex much simpler.
Simple to use
const {RegExpFactory} = require('create-regex');
const regexFactory = new RegExpFactory({ });
const yourRegex = regexFactory.create(({
nonCaptureGroupWithSeperator,
group
})=>
nonCaptureGroupWithSeperator([
group('digits','\\d+'),
group('words', '\\w+'),
group('else', '[\\s\\S]+')
])
,'gm');
const str = '121212ababa...{}';
let match;
while ((match = regex.exec(str)) !== null) {
if (match.index === regex.lastIndex) {
regex.lastIndex++;
}
Object.keys(match.groups).forEach((groupName) => {
if (match.groups[groupName])
console.log(`Found match, group ${groupName}: ${match.groups[groupName]}`);
});
}
API
The API is very simple and yet powerfull.
The factory accepts mixins that you will be able use during create
const regexFactory = new RegExpFactory({
mixins: {
stringSigns: [`'`,'"',"'"]
},
RegExp : RegExp
});
regexFactory.create(({
// Built in helpers
nonCaptureGroupWithSeperator,
group,
wrapPattern,
// User mixins
stringSigns
})=>
nonCaptureGroupWithSeperator([
group('digits',
wrapPattern('\\d+',stringSigns)
),
group('words',
wrapPattern('\\w+',stringSigns)
),
group('else', '[\\s\\S]+')
])
,'gm');
License
MIT