Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

74
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda