5 ms·
Super-expressive: library for building regexes in (almost) natural language
- auganov 6y agoThe rx macro in emacs is similar: https://francismurillo.github.io/2017-03-30-Exploring-Emacs-rx-Macro/ https://francismurillo.github.io/2017-03-30-Exploring-Emacs-...
- IceDane 6y agoThis is a great solution to what is mostly a non-problem. It's also not the first one of its kind. Regular expressions should be used so sparingly that you don't need an entire library to write them safely. If you're ever writing a regular expression and find yourself needing something like this to keep it under control, IMO you should step back and consider just writing some actual code instead. Use a proper parser if you're doing a complicated parsing job, or if you're doing data validation, you can generally just reify your predicates as boolean functions, or you might reach for something like joi or a similar library.
- pubby 6y agoThe syntax seems strange to me. Why require .end() instead of using function parenthesis? .capture.anyOf.range('A', 'F').range('a', 'f').range('0', '9').end().end() Could easily look like: capture(anyOf(range('A', 'F'), range('a', 'f'), range('0', '9')))
- lifthrasiir 6y agoSuperExpressive() .anythingButString('aeiou') .toRegex(); // -> /(?:[^a][^e][^i][^o][^u])/ This is incorrect. It will match "frank" (that would be incorrect if the method is supposed to avoid all vowels) and won't match "aeiuo" (that would be incorrect if the method is supposed to accept anything but the exact string "aeiou"). Assuming the latter was intended, the correct behavior depends on the negation operator which is not natively supported in ECMAScript; simulation is possible (as negation of regular language is still regular) but at that point RegExp tends to be a bad solution.