Regular expressions are on of the most powerful tools in a developer’s toolkit. But let’s be honest, regex kind of sucks to write. Not only is it hard to write, but it’s also hard to read and debug too. So how can we make it easier to use?
In its traditional form, regex defines powerful string patterns in a very compact statement. One trade-off we can make is to use a more verbose syntax that is easier to read and write. This is the purpose of a package like regexpbuilderjs
.
The regexpbuilderjs
package is actually a port of the popular PHP package, regexpbuilderphp. The regexpbuilderphp
package itself is a port of an old JS package, regexpbuilder
, which now seems to be gone. This new package is meant to continue the work of the original regexpbuilder
package.
All credit goes to Andrew Jones for creating the original JS version and Max Girkens for the PHP port.
Installation
To install the package, you can use npm:
$ npm install regexpbuilderjs
Usage
Here’s a simple example of how you can use the package:
const RegExpBuilder = require('regexpbuilderjs');
const builder = new RegExpBuilder();
const regEx = builder
.startOfLine()
.exactly(1)
.of('S')
.getRegExp();
Now let’s break this down a bit. The RegExpBuilder
class is the main class that you’ll be using to build your regular expressions. You can start by creating a new instance of this class and chain methods together to create your regex:
startOfLine()
: This method adds the^
character to the regex, which matches the start of a line.exactly(1)
: This method adds the{1}
quantifier to the regex, which matches exactly one occurrence of a given character or group.of('S')
: This method adds theS
character to the regex.getRegExp()
: This method returns the finalRegExp
object that you can use to match strings.
Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!
With this, you can match strings like “Scott”, “Soccer”, or “S418401”.
This is great and all, but this is probably a regex string you could come up with on your own and not struggle too much to read. So now let’s see a more complex example:
const builder = new RegExpBuilder();
const regExp = builder
.startOfInput()
.exactly(4).digits()
.then('_')
.exactly(2).digits()
.then('_')
.min(3).max(10).letters()
.then('.')
.anyOf(['png', 'jpg', 'gif'])
.endOfInput()
.getRegExp();
This regex is meant to match filenames, which may look like:
- 2020_10_hund.jpg
- 2030_11_katze.png
- 4000_99_maus.gif
Some interesting parts of this regex is that we can specify type of strings (i.e. digits()
), min and max occurrences of a character or group (i.e. min(3).max(10)
), and a list of possible values (i.e. anyOf(['png', 'jpg', 'gif'])
).
For a full list of methods you can use to build your regex, you can check out the documentation.
This is just a small taste of what you can do with regexpbuilderjs
. The package is very powerful and can help you build complex regular expressions in a more readable and maintainable way.
Conclusion
Comments, questions, and suggestions are always welcome! If you have any feedback on how this could work better, feel free to reach out on X. In the meantime, you can check out the repo on GitHub and give it a star while you’re at it.