
Research
Malicious fezbox npm Package Steals Browser Passwords from Cookies via Innovative QR Code Steganographic Technique
A malicious package uses a QR code as steganography in an innovative technique.
ts-regex-builder
Advanced tools
Build maintainable regular expressions for TypeScript and JavaScript.
Regular expressions are a powerful tool for matching text patterns, yet they are notorious for their hard-to-parse syntax, especially in the case of more complex patterns.
This library allows users to create regular expressions in a structured way, making them easy to write and review. It provides a domain-specific langauge for defining regular expressions, which are finally turned into JavaScript-native RegExp
objects for fast execution.
// Regular JS RegExp
const hexColor = /^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/;
// TS Regex Builder DSL
const hexDigit = charClass(
charRange('a', 'f'),
charRange('A', 'F'),
charRange('0', '9'),
);
const hexColor = buildRegExp([
startOfString,
optional('#'),
capture(
choiceOf(
repeat(hexDigit, 6), // #rrggbb
repeat(hexDigit, 3), // #rgb
),
),
endOfString,
]);
npm install ts-regex-builder
or
yarn add ts-regex-builder
import { buildRegExp, capture, oneOrMore } from 'ts-regex-builder';
// /Hello (\w+)/
const regex = buildRegExp(['Hello ', capture(oneOrMore(word))]);
TS Regex Builder allows you to build complex regular expressions using domain-specific language.
Terminology:
RegexConstruct
) - common name for all regex constructs like character classes, quantifiers, and anchors.RegexElement
) - a fundamental building block of a regular expression, defined as either a regex construct, a string, or RegExp
literal (/.../
).RegexSequence
) - a sequence of regex elements forming a regular expression. For developer convenience, it also accepts a single element instead of an array.Most of the regex constructs accept a regex sequence as their argument.
Examples of sequences:
capture('Hello')
'Hello'
RegExp
literal): /Hello/
['USD', oneOrMore(digit), /Hello/]
Regex constructs can be composed into a tree structure:
const currencyCode = repeat(charRange('A', 'Z'), 3);
const currencyAmount = buildRegExp([
choiceOf('$', '€', currencyCode), // currency
capture(
oneOrMore(digit), // integer part
optional(['.', repeat(digit, 2)]), // fractional part
),
]);
See API document.
Builder | Regex Syntax | Description |
---|---|---|
buildRegExp(...) | /.../ | Create RegExp instance |
buildRegExp(..., { ignoreCase: true }) | /.../i | Create RegExp instance with flags |
Construct | Regex Syntax | Notes |
---|---|---|
capture(...) | (...) | Create a capture group |
choiceOf(x, y, z) | x|y|z | Match one of provided sequences |
Quantifier | Regex Syntax | Description |
---|---|---|
zeroOrMore(x) | x* | Zero or more occurence of a pattern |
oneOrMore(x) | x+ | One or more occurence of a pattern |
optional(x) | x? | Zero or one occurence of a pattern |
repeat(x, n) | x{n} | Pattern repeats exact number of times |
repeat(x, { min: n, }) | x{n,} | Pattern repeats at least given number of times |
repeat(x, { min: n, max: n2 }) | x{n1,n2} | Pattern repeats between n1 and n2 number of times |
Character class | Regex Syntax | Description |
---|---|---|
any | . | Any character |
word | \w | Word characters |
digit | \d | Digit characters |
whitespace | \s | Whitespace characters |
anyOf('abc') | [abc] | Any of supplied characters |
charRange('a', 'z') | [a-z] | Range of characters |
charClass(...) | [...] | Concatenation of multiple character classes |
inverted(...) | [^...] | Negation of a given character class |
Anchor | Regex Syntax | Description |
---|---|---|
startOfString | ^ | Match the start of the string (or the start of a line in multiline mode) |
endOfString | $ | Match the end of the string (or the end of a line in multiline mode) |
See Examples document.
Regular expressions created with this library are executed at runtime, so you should avoid creating them in a context where they would need to be executed multiple times, e.g., inside loops or functions. We recommend that you create a top-level object for each required regex.
See the contributing guide to learn how to contribute to the repository and the development workflow. See the project guidelines to understand our core principles.
MIT
TS Regex Builder is inspired by Swift Regex Builder API.
Made with create-react-native-library
FAQs
Maintainable regular expressions for TypeScript and JavaScript.
The npm package ts-regex-builder receives a total of 83,544 weekly downloads. As such, ts-regex-builder popularity was classified as popular.
We found that ts-regex-builder 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.
Research
A malicious package uses a QR code as steganography in an innovative technique.
Research
/Security News
Socket identified 80 fake candidates targeting engineering roles, including suspected North Korean operators, exposing the new reality of hiring as a security function.
Application Security
/Research
/Security News
Socket detected multiple compromised CrowdStrike npm packages, continuing the "Shai-Hulud" supply chain attack that has now impacted nearly 500 packages.