Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

136
Views
Regex group optimization

In the context of using regexp for matching documents against several keywords it could takes quadratic time (number of documents × number of keywords).

With a set of the following keywords:

['ant', 'antelope', 'albatross', 'alligator']

I would produce the stupid regex:

/ant|antelope|albatross|alligator/

But I'm looking for a solution which convert the set into something like:

/a(nt(|elope)|l(batross|ligator))/

which is quite more efficient

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I produced a simple library to make that work available on npm npm i regexp-factorize which can be used as following

import { factorizeOr } from 'regexp-factorize';

const keywords = ['ant', 'antelope', 'albatross', 'alligator'];
const regexpString = factorizeOr(keywords);
const regexp = new RegExp(regexpString);

const document = "There's an alligator in my house";

console.log(regexp);                // /a(?:nt(?:|elope)|l(?:batross|ligator))/
console.log(regexp.test(document)); // true
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!