{"id":53,"date":"2024-06-06T18:37:25","date_gmt":"2024-06-06T18:37:25","guid":{"rendered":"https:\/\/imcodinggenius.com\/?p=53"},"modified":"2024-06-06T18:37:25","modified_gmt":"2024-06-06T18:37:25","slug":"simplify-regular-expressions-with-regexpbuilderjs","status":"publish","type":"post","link":"https:\/\/imcodinggenius.com\/?p=53","title":{"rendered":"Simplify Regular Expressions with RegExpBuilderJS"},"content":{"rendered":"<p>Regular expressions are on of the most powerful tools in a developer&#8217;s toolkit. But let&#8217;s be honest, regex kind of sucks to write. Not only is it hard to write, but it&#8217;s also hard to read and debug too. So how can we make it easier to use?<\/p>\n<p>In its traditional form, regex defines powerful string patterns in a very compact statement. One trade-off we can make is to use a more verbose syntax that is easier to read and write. This is the purpose of a package like regexpbuilderjs.<\/p>\n<div class=\"alert alert-note\">\n<div class=\"flex\">\n<div class=\"flex-shrink-0 mr-3\"><\/div>\n<div class=\"w-full\">\n<p>The regexpbuilderjs package is actually a port of the popular PHP package, <a target=\"_blank\" href=\"https:\/\/github.com\/gherkins\/regexpbuilderphp\/wiki#eitherfindregexpbuilder\" rel=\"noopener\">regexpbuilderphp<\/a>. The regexpbuilderphp package itself is a port of an old JS package, regexpbuilder, which now seems to be gone. This new package is meant to continue the work of the original regexpbuilder package.<\/p>\n<p>All credit goes to <a target=\"_blank\" href=\"https:\/\/github.com\/thebinarysearchtree\" rel=\"noopener\">Andrew Jones<\/a> for creating the original JS version and <a target=\"_blank\" href=\"https:\/\/github.com\/gherkins\" rel=\"noopener\">Max Girkens<\/a> for the PHP port.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<h2>Installation<\/h2>\n<p>To install the package, you can use npm:<\/p>\n<p><span class=\"hljs-meta\">$<\/span><span class=\"bash\"> npm install regexpbuilderjs<\/span><\/p>\n<h2>Usage<\/h2>\n<p>Here&#8217;s a simple example of how you can use the package:<\/p>\n<p><span class=\"hljs-keyword\">const<\/span> RegExpBuilder = <span class=\"hljs-built_in\">require<\/span>(<span class=\"hljs-string\">&#8216;regexpbuilderjs&#8217;<\/span>);<\/p>\n<p><span class=\"hljs-keyword\">const<\/span> builder = <span class=\"hljs-keyword\">new<\/span> RegExpBuilder();<br \/>\n<span class=\"hljs-keyword\">const<\/span> regEx = builder<br \/>\n    .startOfLine()<br \/>\n    .exactly(<span class=\"hljs-number\">1<\/span>)<br \/>\n    .of(<span class=\"hljs-string\">&#8216;S&#8217;<\/span>)<br \/>\n    .getRegExp();<\/p>\n<p>Now let&#8217;s break this down a bit. The RegExpBuilder class is the main class that you&#8217;ll be using to build your regular expressions. You can start by creating a new instance of this class and chain methods together to create your regex:<\/p>\n<p>startOfLine(): This method adds the ^ character to the regex, which matches the start of a line.<br \/>\nexactly(1): This method adds the {1} quantifier to the regex, which matches exactly one occurrence of a given character or group.<br \/>\nof(&#8216;S&#8217;): This method adds the S character to the regex.<br \/>\ngetRegExp(): This method returns the final RegExp object that you can use to match strings.<\/p>\n<p>With this, you can match strings like &#171;Scott&#187;, &#171;Soccer&#187;, or &#171;S418401&#187;.<\/p>\n<p>This is great and all, but this is probably a regex string you could come up with on your own and not struggle too much to read. So now let&#8217;s see a more complex example:<\/p>\n<p><span class=\"hljs-keyword\">const<\/span> builder = <span class=\"hljs-keyword\">new<\/span> RegExpBuilder();<\/p>\n<p><span class=\"hljs-keyword\">const<\/span> regExp = builder<br \/>\n    .startOfInput()<br \/>\n    .exactly(<span class=\"hljs-number\">4<\/span>).digits()<br \/>\n    .then(<span class=\"hljs-string\">&#8216;_&#8217;<\/span>)<br \/>\n    .exactly(<span class=\"hljs-number\">2<\/span>).digits()<br \/>\n    .then(<span class=\"hljs-string\">&#8216;_&#8217;<\/span>)<br \/>\n    .min(<span class=\"hljs-number\">3<\/span>).max(<span class=\"hljs-number\">10<\/span>).letters()<br \/>\n    .then(<span class=\"hljs-string\">&#8216;.&#8217;<\/span>)<br \/>\n    .anyOf([<span class=\"hljs-string\">&#8216;png&#8217;<\/span>, <span class=\"hljs-string\">&#8216;jpg&#8217;<\/span>, <span class=\"hljs-string\">&#8216;gif&#8217;<\/span>])<br \/>\n    .endOfInput()<br \/>\n    .getRegExp();<\/p>\n<p>This regex is meant to match filenames, which may look like:<\/p>\n<p>2020_10_hund.jpg<br \/>\n2030_11_katze.png<br \/>\n4000_99_maus.gif<\/p>\n<p>Some interesting parts of this regex is that we can specify type of strings (i.e. digits()), min and max occurrences of a character or group (i.e. min(3).max(10)), and a list of possible values (i.e. anyOf([&#8216;png&#8217;, &#8216;jpg&#8217;, &#8216;gif&#8217;])).<\/p>\n<p>For a full list of methods you can use to build your regex, you can check out the <a target=\"_blank\" href=\"https:\/\/github.com\/scottwrobinson\/regexpbuilderjs?tab=readme-ov-file#documentation\" rel=\"noopener\">documentation<\/a>.<\/p>\n<p>This is just a small taste of what you can do with regexpbuilderjs. The package is very powerful and can help you build complex regular expressions in a more readable and maintainable way.<\/p>\n<h2>Conclusion<\/h2>\n<p>Comments, questions, and suggestions are always welcome! If you have any feedback on how this could work better, feel free to <a target=\"_blank\" href=\"https:\/\/twitter.com\/ScottWRobinson\" rel=\"noopener\">reach out on X<\/a>. In the meantime, you can check out the repo on <a target=\"_blank\" href=\"https:\/\/github.com\/scottwrobinson\/regexpbuilderjs\" rel=\"noopener\">GitHub<\/a> and give it a star while you&#8217;re at it.<\/p>","protected":false},"excerpt":{"rendered":"<p>Regular expressions are on of the most powerful tools in a developer&#8217;s toolkit. But let&#8217;s be honest, regex kind of sucks to write. Not only is it hard to write, but it&#8217;s also hard to read and debug too. So how can we make it easier to use? In its &#8230; <\/p>\n<div><a class=\"more-link bs-book_btn\" href=\"https:\/\/imcodinggenius.com\/?p=53\">Read More<\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-53","post","type-post","status-publish","format-standard","hentry","category-news"],"_links":{"self":[{"href":"https:\/\/imcodinggenius.com\/index.php?rest_route=\/wp\/v2\/posts\/53"}],"collection":[{"href":"https:\/\/imcodinggenius.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/imcodinggenius.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/imcodinggenius.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/imcodinggenius.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=53"}],"version-history":[{"count":0,"href":"https:\/\/imcodinggenius.com\/index.php?rest_route=\/wp\/v2\/posts\/53\/revisions"}],"wp:attachment":[{"href":"https:\/\/imcodinggenius.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=53"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/imcodinggenius.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=53"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/imcodinggenius.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=53"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}