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

72
Views
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 answers
Answer question

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 Report

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 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!