Created by Ridwan Ajibola
Sick of trying to understand those confusing regex patterns? Let's change that.
// Before: Cryptic regex for password validation
const passwordRegex = /^(?=.*\d)(?=.*[!@#$%^&*])(?=.*[a-zA-Z]).{8,}$/;
// After: Human-readable with human-regex
const humanPasswordRegex = createRegex()
.startAnchor()
.hasDigit() // (?=.*\d)
.hasSpecialChar() // (?=.*[!@#$%^&*])
.hasLetter() // (?=.*[a-zA-Z])
.anyCharacter() // .
.atLeast(8) // {8,}
.endAnchor()
.toRegExp();
Why I Built This
Regex is powerful, but let’s be honest: patterns like /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/
look like someone fell asleep on their keyboard. When I first learned JavaScript, regex was my #1 frustration – and every developer I asked shared this pain.
So I asked: What if we could write regex in plain English?
Human-Regex was born, created by Ridwan Ajibola, a utility library that turns regex patterns into chainable, self-documenting code.
How It Works
Traditional Regex → Human-Regex
Traditional Regex | Human-Regex Equivalent |
---|---|
/\d/ |
.digit() |
/[a-zA-Z]/ |
.letter() |
/(?=.*\d)/ |
.hasDigit() |
^ / $ |
.startAnchor() / .endAnchor() |
const emailRegex = createRegex()
.startAnchor()
.word().oneOrMore() // [a-zA-Z0-9_]+
.literal('@') // @
.word().oneOrMore() // [a-zA-Z0-9_]+
.literal('.') // .
.letter().atLeast(2) // [a-zA-Z]{2,}
.endAnchor()
.toRegExp();
No more guessing what [a-zA-Z0-9_]
or {2,}
means. The code explains itself.
Key Features
✅ Readable Syntax
Methods like .hasDigit()
and .startAnchor()
make patterns self-documenting.
✅ Chainable Design
Build complex patterns step-by-step, just like writing a sentence.
✅ Lightweight
Just 1.0 kB gzipped. (1.4kB new version)
✅ Open Source
MIT licensed. Contributions welcome!
Get Started in 60 Seconds
Install the library:
npm install human-regex
Build your first regex:
const urlRegex = createRegex()
.startAnchor()
.protocol() // https?://
.www().optional() // (www\.)?
.word().oneOrMore()
.literal('.')
.tld() // com|org|net
.toRegExp();
Why This Matters
Onboarding: New developers understand regex logic at a glance.
Maintenance: No more regex archaeology to update old code.
Collaboration: Teams spend less time decoding patterns.
Join the Movement
Regex doesn’t have to be scary. Try Human Regex and:
⭐ Star the GitHub repo — it really helps! 🚀
🐞 Report bugs or request features
💡 Contribute code or documentation
Let’s make regex accessible to everyone!
Top comments (23)
Good work, but I missing the captialLetter() function because now in this password example can be passed without using capitalLetter.
If you try to make a harder regexp example, sure to found a few more missing function.
Check this code: github.com/Pengeszikra/flogon-gala...
it is a markdown parser part of this game: dev.to/pengeszikra/javascript-grea... , try to recreate those regexp with your module and you will be found a missings.
Good! I will make sure to include the missing methods in the new release.
Thanks
Skill issues to be honest
Thanks
I’d appreciate it if you could star the repo.
this is cool man, like really cool...
Thanks a lot @msamgan! Your contributions are really appreciated. If you find this project useful, it would mean a lot if you could star the repo, it helps others discover it too!
Thanks a lot @msamgan, I’d appreciate it if you could star the repo.
Man, this is just amazing, gonna start using this, big applause
Really appreciate it. I’d love it if you could star the repo; it helps a lot!
Finally i won't get the excuse by my peers that regex is too hard!!
Great work!!!
I’d really appreciate it if you could star the repo—it would be a huge help!
Thanks!
Wow! This is a great discovering and an insight to the developers world!
Thanks
I’d appreciate it if you could star the repository
Great work, sir.
Thanks
Like this project!
I see a bug / inconsistency:
If
.digit()
is matching\d
then it converts toDecimal_Number
Unicode property. For example (I'm not familiar with JS so I'll use Raku):Then
.letter()
should convert consequently toLetter
Unicode property, nota-z
. For example:You should not make implicit ASCII / non-ASCII assumptions, where one method works differently than the other sibling.
Another bug you have is anchoring:
Token
$
means end of logical string. What you are probably looking for is\z
:I don't want to discourage you, but I really dislike those "regex to human" modules. They make code crazy error-prone, because - as I just shown - you don't see explicitly what you are matching. Things get worse when you are working on multi language stack and you want to exchange your PCRE regexps with someone using other language. Basically all "Why This Matters" points are just the opposite - new developers will not understand regexes more, there will be more archeology because you will need to decipher additional layer of abstraction, and collaboration will be more difficult.
My advice would be to stick directly (or at least closely) to Unicode properties. Drop ambiguous method
letter()
and addUppercase_Letter()
mapping directly toLu
property. And build modifiers on top of that like Uppercase_Letter('ascii')or
Uppercase_Letter('script'=>'Latin')`. Otherwise this will be false friend - module that is supposed to make your life easier but it introduces weird errors and security risks because it hides too much assumptions under the hood.This is explicitly for JavaScript and not for any languages
Sure. I pointed out universal issues. Imagine developer joining some project that uses this module. If he/she already has regular expression experience this interface will be confusing, because your assumption of what "letter" or "endAnchor" are is completly different than what those things mean in terms of Unicode properties and PCRE standard.
Same goes for "tld". Your module does not match TLDs. It only matches what you consider to be TLD. Exactly 3 items out of 1589 currently known TLDs, so right out of the box it has 99.81% failure rate.
I'm not trying to be mean, I'm just saying that pseudo-standards or partially implemented specs are universally bad and sooner or later backfire in every project.
I never really understood whats so hard about Regex? I think if you just learn the grammar once you dont really forget it anymore and its perfectly understandable. Nice idea anyway:)
Thanks Jonas