Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

251
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda