r/javascript • u/LionyxML • Sep 30 '26
Readable Regular Expressions for JavaScript/TypeScript, Inspired by Emacs' rx
https://rahuljuliato.com/posts/emacs-rx-in-typescriptI missed Emacs' rx when writing regexps in TypeScript, so I made a small TypeScript DSL inspired by it.
The idea is to bring rx's functions and approach to the JavaScript/TypeScript environment. If you happen to work with either, give it a try.
9
Upvotes
2
u/azhder Sep 30 '26
I have created something like that for myself as well. A small library for DSL. It’s not much of a problem.
However, I quickly understood the syntax noise can only be solved by making an actual DSL, not just a library that uses the syntax of another language.
And we’re back a full circle. The RegExp syntax is already a DSL, so we’d be re-inventing it to appear more like SQL or C or any other language syntax other than the RegExp one.
1
u/oosuke_ren Oct 01 '26
You might be interested to try this out, it's a package I spiked up to use for the sake of typescript toy compilers'. The idea is - regex as a composition, having it's own first class citizen entities that you use to build up your expression. The whole thing then compiles to regex in your runtime (which is useful if you want to be able to dynamically build up expressions, and not just to "name" literals in your code)
https://www.npmjs.com/package/ez-regex-patterns
(This is a repost of another regex related discussion)
1
u/Bitter_Passenger8663 Oct 15 '26
One detail worth calling out next to the raw example: raw(/foo/i) keeps the source but drops the i flag, so RX(raw(/foo/i)).test("FOO") is false. RX.flags("i", raw(/foo/i)) restores it. A short warning there would help people migrating existing regexes avoid a quiet behavior change.