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

111
Vistas
How to match a string from array of strings using javascript and regex?

Hi below is an array of strings,

const arr = [
    "list-domain/1/",
    "list-domain/1/list/1",
    "some-group/2/",
    "some-group/2/list/2",
    "list/3/",
    "list/3/item/1"
];

I want to return true if the string matches "list/1/" or "list-domain/2/list/2/" or "some-group/3/list/4"; so basically should return true if string starts with "list/1/" or has some string in front and ends with "/list/1/"

note: here the number after slash can be any number.

so for the above array expected output is true.

 const arr1 = [
    "list-domain/1/",
    "list-domain/1/list/1",
    "some-group/2/",
    "some-group/2/list/2",
];

for arr1 expected output true

 const arr2 = [
    "list-domain/1/",
    "list-domain/1/list/1/item/2",
    "some-group/2/",
];

for arr2 expected output is false.

i have tried something like below,

const foundMatch = arr.some(a=> new RegExp(/(\/list\/[0-9]+)?\/$/g).test(a));

but this doesn't work

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Try this:

arr.some(a=> new RegExp(/(^list\/\d\/)|(list\/\d\/$)/).test(a));

If that's what you mean by:

If string starts with "list/1/" or has some string in front and ends with "list/1/".

about 4 years ago · Juan Pablo Isaza Denunciar

0

Is this what you want to have?

arr.some(a=> new RegExp(/list\/[0-9](\/)?$/g).test(a));

Your above all cases fulfills by this regex. If not you can put other test cases.

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