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

88
Views
How to match particular strings in an array using javascript?

i have an array of strings like below,

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

as seen from above array i want to check if the array contains strings of kind

"list-domain/1/" or "some-group/2/" or "list/3/"

here the numbers after / can be anything. meaning in "list-domain/1" here 1 can be 2 or 3 anynumber i have to check if the string contains list-domain or some-group or list followed by / and any number followed by /

so for the above array the expected output is true.

now consider array below,

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

for arr1 the expected output is false althought there is strings list-domain/1, some-group/2 and list/3 but they are followed by someother strings too like list-domain/1/list/1, some-group/2/list/2, list/3/item/1

so i want to find the exact strings numbers can be anything after /. could someone help me how to do this. thanks.

EDIT:

i want it to return true if the string has "list/1/" or

any string followed by "list/1" but return false if "list/1" is followed by someother string like below

 "list-domain/1/list/1/item/1"

true in cases below

"list-domain/1/list/1"
"list/2"
"some-group/3/list/3"
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Use a regular expression. Here is an example.

const rx = /^(([^\/]+)\/(\d+)\/)+$/
// or without the groups
const rxNoGroups = /^([^\/]+\/\d+\/)+$/

const found = items.filter(it => rx.test(it))

This regex tests

  • start of input
  • not-a-slash
  • slash
  • number
  • slash
  • end of input or next group

Tailor it to your needs. I coded it so your non-number words can still contain numbers, so foo/2/bar3/4/. You can also limit number of repetitions.

You can also just test that the string ends with /.

about 4 years ago · Juan Pablo Isaza Report

0

This gives you the required output using Regular Expression (RegExp):

arr.some(a=> new RegExp(/^(list-domain|some-group|list)\/[0-9]+\/$/g).test(a))
about 4 years ago · Juan Pablo Isaza Report

0

If you can count on the shape of pattern being always a string identifier followed by a numerical id a parser might look as follows:

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

const parse = (str) => {
    const parts = str.split('/');
    return parts.reduce((parsed, part, i, parts) => {
        if (part === '') return parsed;
        if (i % 2 === 0) {
            parsed[part] = null;
        } else {
            parsed[parts[i-1]] = part;
        }
        return parsed;
    }, {});
}

arr.forEach(s => {
    console.log(parse(s));
})

// returns:
// {list-domain: "1"}
// {list-domain: "1", list: "1"}
// {some-group: "2"}
// {some-group: "2", list: "2"}
// {list: "3"}
// {list: "3", item: "1"}

It returns an object where keys are the string identifiers and values numerical ids. However, as I said, it relies on the exact shape of the string and regular alteration of an identifier and a value.

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!