https://github.com/markwhitaker/regextoolbox.js
Regular expression tools for JavaScript developers.
https://github.com/markwhitaker/regextoolbox.js
javascript javascript-library pattern-matching regex regexes regexp regular-expression regular-expressions
Last synced: 2 months ago
JSON representation
Regular expression tools for JavaScript developers.
- Host: GitHub
- URL: https://github.com/markwhitaker/regextoolbox.js
- Owner: markwhitaker
- License: apache-2.0
- Created: 2017-08-11T15:32:03.000Z (almost 8 years ago)
- Default Branch: main
- Last Pushed: 2024-08-20T00:28:27.000Z (10 months ago)
- Last Synced: 2024-12-06T22:03:44.728Z (7 months ago)
- Topics: javascript, javascript-library, pattern-matching, regex, regexes, regexp, regular-expression, regular-expressions
- Language: JavaScript
- Homepage:
- Size: 370 KB
- Stars: 6
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

# RegexToolbox.js [](https://github.com/markwhitaker/RegexToolbox.js/actions/workflows/build-and-test.yml) [](https://github.com/markwhitaker/RegexToolbox.js/actions/workflows/publish-to-npm.yml) [](https://www.npmjs.com/package/regextoolbox) [](https://www.npmjs.com/package/regextoolbox)
Regular expression tools for JavaScript developers.
## Installation
```
npm i regextoolbox
```## RegexBuilder
`RegexBuilder` is a class for building regular expressions in a more human-readable way using a fluent API. It offers a number of benefits over using raw regex syntax in strings:
- No knowledge of regular expression syntax is required: just use simple, intuitively-named classes and methods.
- Code is easier to read, understand and maintain.
- Code is safer and far less prone to regular expression syntax errors and programmer errors.It is fully documented in the [project wiki](https://github.com/markwhitaker/RegexToolbox.JS/wiki).
## ⚠️ Breaking changes in 2.0
### Improved group syntax
The grouping methods have been totally overhauled in version 2.0 to make them easier to use and less error-prone.
Go from this:
```javascript
const regex = new RegexBuilder()
.startGroup()
.digit()
.letter()
.buildRegex(); // ERROR: forgot to call endGroup()
```To this:
```javascript
const regex = new RegexBuilder()
.group(r => r
.digit()
.letter()
)
.buildRegex();
```Details of breaking changes:
| Removed | Replaced with |
|-------------------------------------------|-----------------------|
| `startGroup() ... endGroup()` | `group()` |
| `startNonCapturingGroup() ... endGroup()` | `nonCapturingGroup()` |### `butAsFewAsPossible` is now a getter
Go from this:
```javascript
RegexQuantifier.oneOrMore.butAsFewAsPossible()
```to this:
```javascript
RegexQuantifier.oneOrMore.butAsFewAsPossible
```## Also new in 2.0
### Support for named groups
Support for named groups has been added. Where the captured values for regular groups are retrieved like this:
```javascript
const regex = new RegexBuilder()
.group(r => r
.uppercaseLetter()
)
.lowercaseLetter(RegexQuantifier.oneOrMore)
.buildRegex();const initial = regex.exec("Mark")[1]; // "M"
```the captured values for named groups are retrieved like this:
```javascript
// Get a number using a group of digits
const regex = new RegexBuilder()
.namedGroup("initialLetter", r => r
.uppercaseLetter()
)
.lowercaseLetter(RegexQuantifier.oneOrMore)
.buildRegex();const initial = regex.exec(input).groups.initialLetter; // "M"
```## Also for JavaScript developers
 [MimeTypes.js](https://github.com/markwhitaker/MimeTypes.js): MIME type constants for your JavaScript projects
## RegexToolbox for other languages
 [RegexToolbox for .NET](https://github.com/markwhitaker/RegexToolbox.NET)
 [RegexToolbox for Java](https://github.com/markwhitaker/RegexToolbox.Java)
 [RegexToolbox for Kotlin](https://github.com/markwhitaker/RegexToolbox.kt)
---
###### **RegexToolbox:** Now you can be a [hero](https://xkcd.com/208/) without knowing regular expressions.