Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Super-expressive: library for building regexes in (almost) natural language (github.com/francisrstokes)
19 points by kiyanwang on July 20, 2020 | hide | past | favorite | 4 comments


This 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.



    SuperExpressive()
      .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.


The 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')))




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: