paint-brush
How to Make Regex Readable In JavaScript by@bob.js
721 reads
721 reads

How to Make Regex Readable In JavaScript

by Bob FornalNovember 25th, 2020
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

The project is to make Regular Expressions easier to understand and reason about. The project will allow a developer to use a Template Literal to build a regex, with comments to make it more readable. The process should be simple and easy to work with. The article How to Make Regex Readable In JavaScript by Bob Fornal LinqTags is published on Hashnode.js. The Project: Reggie Docs is the first time I have submitted my own project to. npmjs.

Company Mentioned

Mention Thumbnail
featured image - How to Make Regex Readable In JavaScript
Bob Fornal HackerNoon profile picture

Reggie-Docs

"My first foray into Open Source development."

Now, this is not the first time I've contributed to open-source software. It is the first time I have submitted my own project to .

The Project: Reggie Docs

Regular Expressions are complicated to write.

It's even more difficult to reason about them; especially if you have to read someone else's code.

About a year ago, I wrote an article about an experiment I was working with (see it ). I've actually used this pattern several times since writing the article and working through the code.

Then, someone said, "this should be an open-source library."

And ... something in my brain clicked.

And ... I started reworking the codebase I had into something more functional.

The Open-Source Project

The project is here: .

This project will allow a developer to use a Template Literal to build a regex, with comments to make it more readable.

const code0001 = `
  /* Matches text avoiding additional spaces
  */
  ^       // Beginning of line
  [\\s]*  // Zero or more whitespace
  (.*?)   // Any characters, zero to unlimited,
          //   lazy (as few times as possible, expanding as needed)
  [\\s]*  // Zero or more whitespace
  $       // End of line
`;

... rather than ...

const code0001regex = /^[\s]*(.*?)[\s]*$/;

Here's a pattern for use of the

code0001
above ...

const Reggie = require('reggie-docs');
const reggie = new Reggie();
 
let results = {};
 
const patternCheck0001 = '  Test Spaces Before and After   ';
 
results.code0001 = reggie.create(code0001);
const code0001Exp = reggie.generate(code0001);
results.code0001Test = code0001Exp.exec(patternCheck0001)[1];

Conclusion

The whole concept of this project is to make Regular Expressions easier to understand and reason about. In doing this, the process should be simple and easy to work with.

Also published .

바카라사이트 바카라사이트 온라인바카라