tink / links #systemcrafters

< Lionyx > dropped a link

Readable Regular Expressions for JavaScript/TypeScript, Inspired by Emacs' rx

https://rahuljuliato.com/posts/emacs-rx-in-typescript

Rahul M. Juliato presents a small TypeScript/JavaScript library that ports Emacs Lisp’s rx macro into a composable DSL for building regular expressions, aiming to replace unreadable regex strings with readable code trees. The implementation treats regex components as objects with specific "kinds" (atom, seq, alt) to handle escaping and grouping logic automatically, ensuring that plain strings are treated as literals and alternations are bracketed correctly when nested. A standout feature is the or function, which sorts string branches by length to mimic Emacs’s regexp-opt behavior, preventing JavaScript’s default left-to-right matching from incorrectly capturing shorter prefixes like "in" instead of "interface". The author demonstrates the utility by rewriting the complex SemVer validation regex into a structured, named-group definition that is significantly easier to audit and modify.

The post includes a full technical breakdown of the engine, covering character set merging, lazy quantifiers, and backreferences, while candidly noting limitations such as ASCII-only character classes (unlike Emacs’s Unicode support) and the absence of syntax-table-dependent forms. It also provides a cheat sheet mapping Emacs rx forms to their TypeScript equivalents and a raw escape hatch for integrating legacy regexps. The piece is a solid, practical tutorial for developers who find standard JavaScript regex syntax brittle, offering a working gist and source code rather than just conceptual examples.

what others said

Hacker News 3 points · 1 comments

The thread was effectively empty, containing only the author’s self-promotional link and no substantive discussion. There were no objections, benchmarks, or alternatives named, nor did anyone contradict the premise of bringing Emacs rx to TypeScript. The room didn’t really exist; it was just a silent post.