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

73
Vistas
How to get particular part of string from an array of strings using javascript?

Hi below is array of strings

const arr = [
    "list/1/item/1/",
    "some-domain/2/item/2/",
    "item/3/",
    "some-domain/4/item/5/subitem/1/",
]

i have to filter those strings that start with string "item/3/" or ends with string "item/3/"

so from above array expected filtered array is like below,

const filtered_arr = [
    "list/1/item/1/",
    "some-domain/2/item/2/",
    "item/3/",
]

from the filtered array i want to get the number after "/item/". so from above filtered array the expected output is

const arr_ids = ["1", "2", "3"]

what i have tried,

i have used below to match those strings that start or end with /item/3/

const filtered_arr = arr.map(str) => {
    return str.match(/item\/[0-9](\/)?$/g);
}

this gives the filtered_arr

const filtered_arr = [
    "list/1/item/1/",
    "some-domain/2/item/2/",
    "item/3/",
]

but how do i map through each array item and get the number after string "/item/".

could someone help me with this. thanks.

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

0

Use filter to filter paths either starting or ending in item/\d+/. Then use map to extract the item number from each matching path.

const arr = [
    "list/1/item/1/",
    "some-domain/2/item/2/",
    "item/3/",
    "some-domain/4/item/5/subitem/1",
];
var output = arr.filter(x => x.match(/^item\/\d+|\bitem\/\d+\/$/))
                .map(x => x.match(/(?<=^item\/)\d+|(?<=\bitem\/)\d+(?=\/$)/)[0]);
console.log(output);

about 4 years ago · Juan Pablo Isaza Denunciar

0

This may help:

const filtered_arr = arr.map(str) => {
    const match = str.match(/\/item\/(\d)/);
    return(match[1]);
}
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