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

247
Views
Cann't combine two regexp in str.match() to get combined results

If I clearly understand then in this example match finds all strings with length 0. And empty strings are in the beginning, ending and between all symbols in string so there are 11 empty string

const str = '0123456789';
const regexp = /|/g;
const matches = str.match(regexp);

console.log('Length = ' + matches.length);
console.log(matches);

In this example everything is clear. I just search digits so there are 10 digits:

const str = '0123456789';
const regexp = /\d/g;
const matches = str.match(regexp);

console.log('Length = ' + matches.length);
console.log(matches);

But I don't understand why I cann't get 21 matches when trying to combine 2 regexp:

const str = '0123456789';

const regexp1 = /|\d/g;
const matches1 = str.match(regexp1);
console.log('Length = ' + matches1.length);
console.log(matches1);

const regexp2 = /\d|/g;
const matches2 = str.match(regexp2);
console.log('Length = ' + matches2.length);
console.log(matches2);

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

0

Why can't I get 21 matches when trying to combine the 2 regexp?

Because .match() returns non-overlapping matches. It will not cover the same character (in each position) with multiple matches, and for empty matches it will not return multiple matches starting at the same index.

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!