Security News
Opengrep Emerges as Open Source Alternative Amid Semgrep Licensing Controversy
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
regexp-manager
Advanced tools
$ npm i regexp-manager
This library creates regular expressions in a form similar to query builders. The benefits of this library are three.
import { RegExpPatternBuilder } from 'regexp-manager';
/**
* result : '(010|011)[0-9]{3,4}[0-9]{4,4}'
*/
const koreanPhoneNumber = new RegExpPatternBuilder()
.capturing((qb) => qb.and('010').or('011')) // '(010|011)'
.and('-') // '-'
.and((qb) => qb, and('[0-9]').between(3, 4)) // '[0-9]{3,4}'
.and('-') // '-'
.and((qb) => qb.and('[0-9]').between(4, 4)).expression; // '[0-9]{4,4}'
If you write a function that returns the string or additional builder according to the inferred type, you can check the result at the time of compilation. If you want to use a sub-builder inside, you can use the qb
given as a parameter. It works even if you just write it as a string.
It means 'or syntax' using pipe characters in regular expressions. The 'or syntax' can also be checked in advance by type level.
// It's also inferred from the type.
const leftOrRight = new RegExpPatternBuilder('left').or('right').expression; // 'left|right'
const redOrBlue = new RegExpPatternBuilder('red').or('blue').expression; // 'red|blue'
const dogOrCat = new RegExpPatternBuilder('dog').or('cat').expression; // 'dog|cat'
The 'and syntax' simply connects strings. In a regular expression, sometimes it is more readable to divide and combine characters in semantic units.
// It's also inferred from the type.
const leftOrRight = new RegExpPatternBuilder('left').and('right').expression; // 'leftright'
const redOrBlue = new RegExpPatternBuilder('red').and('blue').expression; // 'redblue'
const dogOrCat = new RegExpPatternBuilder('dog').and('cat').expression; // 'dogcat'
There is a syntax called 'capturing' in the regular expression. This syntax uses parentheses to lock the string, making the test
, match
method of the regular expression different in the future.
// It's also inferred from the type.
const capturingA = new RegExpPatternBuilder().capturing('A').expression; // '(A)'
There are four methods in quantifier
: lessThan
, lessThanOrEqual
, moreThan
, and moreThanOrEqual
. They define how many times a previously specified character or string will exist based on a given number.
if you want, you can use between
. it will be easy to use builder.
// `lessThan` method
// Below return 'a{1,9}'. And it's also inferred from the type.
const lessThanTen = new RegExpPatternBuilder('a').lessThan(10).expression;
// `lessThanOrEqual` method
// Below return 'a{1,10}'. And it's also inferred from the type.
const lessThanOrEqualTen = new RegExpPatternBuilder('a').lessThanOrEqual(10).expression;
// `moreThan` method
// Below return 'a{4,}'. And it's also inferred from the type.
const moreThanThree = new RegExpPatternBuilder('a').moreThan(3).expression;
// `moreThanOrEqual` method
// Below return 'a{3,}'. And it's also inferred from the type.
const moreThanOrEqualThree = new RegExpPatternBuilder('a').moreThanOrEqual(3).expression;
It means lookahead
, lookbehind
of the regular expression. These are strings that should be included in a string but should not be caught in a regular expression.
// `include`("left", P) method means lookbehind
// Below return '(?<=a)b'. And it's also inferred from the type.
const lookhehind = new RegExpPatternBuilder('b').includes('LEFT', 'a').expression;
// `include`("right", P) method means lookahead
// Below return 'b(?=a)'. And it's also inferred from the type.
const lookahead = new RegExpPatternBuilder('b').includes('RIGHT', 'a').expression;
It means lookahead
, lookbehind
of the regular expression. These are strings that should be excluded in a string.
// `exclude`("left", P) method means negative lookbehind
// Below return '(?<!a)b'. And it's also inferred from the type.
const nagativeLookbhind = new RegExpPatternBuilder('b').excludes('LEFT', 'a').expression;
// `exclude`("right", P) method means negative lookbehind
// Below return 'b(?!a)'. And it's also inferred from the type.
const negativeLookahead = new RegExpPatternBuilder('b').excludes('RIGHT', 'a').expression;
const builder = new RegExpPatternBuilder('abc').beginning().expression; // '^abc'
const expression = new RegExpPatternBuilder().range('1-10').expression; // '1-10'
const expression = new RegExpPatternBuilder().range(1, 10).expression; // '1-10'
const expression = new RegExpPatternBuilder().optional('abc').expression; // 'abc?'
FAQs
regexp builder for node.js developer
The npm package regexp-manager receives a total of 46 weekly downloads. As such, regexp-manager popularity was classified as not popular.
We found that regexp-manager demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.