Welcome to the Off-Shore Club

The #1 Social Engineering Project in the world since 2004 !

Simplify Regular Expressions with RegExpBuilderJS

Gold

AidenParker

Regular Hacker
🔥 Businessman 🔥
USDT(TRC-20)
$0.0
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.

icon-information-circle-solid.svg


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 the original JS version and Max Girkens for the PHP port.

Installation​


To install the package, you can use npm:

Code:
$ npm install regexpbuilderjs

Usage​


Here's a simple example of how you can use the package:

Code:
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 the S character to the regex.
  • getRegExp(): This method returns the final RegExp object that you can use to match strings.

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:

Code:
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.
 

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.

Friendly Disclaimer We do not host or store any files on our website except thread messages, most likely your DMCA content is being hosted on a third-party website and you need to contact them. Representatives of this site ("service") are not responsible for any content created by users and for accounts. The materials presented express only the opinions of their authors.
🚨 Do not get Ripped Off ! ⚖️ Deal with approved sellers or use RTM Escrow on Telegram
Gold
Mitalk.lat official Off Shore Club Chat


Gold

Panel Title #1

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

Panel Title #2

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Top