swyft-regex
v1.0.4
Published
RegexBuilder is a JavaScript library that provides a convenient way to build regular expressions using a fluent and chainable API.
Downloads
14
Readme
RegexBuilder
RegexBuilder is a JavaScript library that provides a convenient way to build regular expressions using a fluent and chainable API.
Installation
You can install RegexBuilder using npm:
npm install regex-builder
Usage
// Import RegexBuilder
import { RegexBuilder } from 'swyft-regex';
// Create a RegexBuilder instance
const regex = new RegexBuilder('abc')
.digit()
.multiple()
.or('def', 'ghi')
.build();
// Use the generated RegExp
const result = regex.test('abc123'); // true
console.log(regex); // /abc\d+(def|ghi)/
API Reference
constructor(regex: string)
Creates an instance of RegexBuilder
includes(char: string): RegexBuilder
Appends a character to the regular expression
onlyOne(): RegexBuilder
Adds {1}
to the regular expression, allowing only one occurrence
multiple(): RegexBuilder
Adds +
to the regular expression, allowing multiple occurrences
atLeast(count: number): RegexBuilder
Adds {count,}
to the regular expression, specifying a minimum occurrence count
atMost(count: number): RegexBuilder
Adds {0, count}
to the regular expression, specifying a maximum occurrence count
anyOf(chars: string): RegexBuilder
Adds a character class [chars]
to the regular expression
digit(): RegexBuilder
Adds \d
to the regular expression, matching any digit character
nonDigit(): RegexBuilder
Adds \D
to the regular expression, matching any non-digit character
word(): RegexBuilder
Adds \w
to the regular expression, matching any word character
nonWord(): RegexBuilder
Adds \W
to the regular expression, matching any non-word character
whitespace(): RegexBuilder
Adds \s
to the regular expression, matching any whitespace character
nonWhitespace(): RegexBuilder
Adds \S
to the regular expression, matching any non-whitespace character
startOfInput(): RegexBuilder
Adds ^
to the beginning of the regular expression, indicating the start of input
endOfInput(): RegexBuilder
Adds $
to the end of the regular expression, indicating the end of input
or(...patterns: (string | RegexBuilder)[]): RegexBuilder
Adds a group with |
for alternation between the current pattern and other patterns
alpha(): RegexBuilder
Adds [a-zA-Z]
to the regular expression, matching any alphabetical character
caseInsensitive(): RegexBuilder
Adds i
to the regular expression, making it case-insensitive
escape(str: string): string
Escapes special characters in a given string
global(): RegexBuilder
Adds g
to the regular expression, making it global
multiline(): RegexBuilder
Adds m
to the regular expression, making it multiline
reset(): RegexBuilder
Resets the regular expression to an empty string
comment(commentText: string): RegexBuilder
Adds a comment to the regular expression using the (?:commentText)
syntax
build(): RegExp
Builds a RegExp object based on the current regular expression string.
License
This project is licensed under the MIT License.