DEV Community

Narender Saini
Narender Saini

Posted on

How to write regex in natural language

How to write regex in natural language

Everyone knows regex is most powerful feature of JavaScript but at the same time it can give hard time to even experienced developers. Reading your regex after few months sometime become too difficult. In today article we will gonna learn how we can write regex in natural language.

Super expressive

Two days ago a new game changing library came into reality. This is a JavaScript library that allows you to build regular expressions in almost natural language – with no extra dependencies, and a lightweight code footprint (less than 3kb with minification + gzip!).

On first place why we need a new library for regex. The answer is simple even the regex is so powerful but writing syntax of regex is too complicated. Most of the time we again need to read the docs of regex to create a new regex.

This library solves the issue of complex syntax. It uses the normal natural language words to create a regex.


Like any other npm package you can install this library using npm or yarn.

npm i super-expressive --save


To use this library first of all you need to import this library.

const SuperExpressive = require('super-expressive');


Find Mutiple hello in a string.

// ->

Find CaseInsenstive Hello.

// ->

Captures the value of a 16-bit hexadecmal number like 0xC0D3

const SuperExpressive = require('super-expressive');

const myRegex = SuperExpressive()
      .range('A', 'F')
      .range('a', 'f')
      .range('0', '9')

// Produces the following regular expression:

Similarly you can create any regex in natural language using this library. I hope you have learned how to write regex in natural language.

Full docs

How to check an element is in viewport using Intersection Observer API

Top comments (7)

jessekphillips profile image
Jesse Phillips

I don't think this really removes the challenges of reading a regular expression. If you're already familiar with regex the is some benefits for things like the non-capturing group.

But when you need to go back and modify a regex it is more nuanced than just "I don't remember what (?: is" all of the tool to check your regex against inputs can be thrown out when using this syntax.

Most of what I have to say were addressed by Adam and DGM, but I don't think they cover your second point very well.

"how about all the very easy to understand, non compact, non cryptic and dare I say pretty domain languages out there like SQL or LINQ?"



caiangums profile image
Ilê Caian

Nice article!

I think that maybe this lib can be used as a first step in getting to know how Regular Expressions works and how to write them!

Personally, I think that RegEx is a powerful tool that can be used in string manipulation and even in Text Editors like Sublime and VSCode to power up your search, replace and modify flow.
Don't underestimate it and remember: Once learned, you can use in almost all languages! 😄

lrdiv profile image
Lawrence Davis • Edited

This seems extremely similar to the VerbalExpressions library that's been around for quite a while in many languages

mxldevs profile image
MxL Devs • Edited

I think it's quite nice. Providing a separate layer of abstraction so that people can just worry about what they want, as opposed to how to get it.

It seems like a pretty natural way that humans come up with regexes, especially if they think in terms of state machines.

It's sort of like, why bother with high-level programming when we could all just learn assembler?

lifelongthinker profile image

Nice solution! This is the kind of pattern that I know under the name of "fluent interface". It works well for DSLs such Regex, LINQ, Logic, ...

deta19 profile image

this...THIS ould be usefull to generate everything you need

rdewolff profile image

Very interesting take on making regex easier to read and share.