0

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"
stackuser
  • 374
  • 2
  • 9
  • *" I want to check if the array contains strings of kind "list-domain/1/" or "some-group/2/" or "list/3/" "*. Do you want to match the exact string `"list-domain/1/"` or will the input be something like `["list-domain", "some-group", "list"]` – adiga Feb 04 '22 at 10:53
  • yes i want to match the exact string. and no the input will not like you mentioned. it will be followed by / and some number and / so strings would be like list-domain/1/ or some-group/1/ or list/2/. i am interested in these strings – stackuser Feb 04 '22 at 10:59
  • i also want to return true if the string "list/2/" has some other string before. so it should return true also for these strings too "some-group/2/list/2/" , "list-domain/4/list/3/", "list/2/" – stackuser Feb 04 '22 at 11:24

3 Answers3

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 /.

Steven Spungin
  • 27,002
  • 5
  • 88
  • 78
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))
RaiBnod
  • 2,141
  • 2
  • 19
  • 25
  • along with this how can i also match string that contains like "some-group/2/list/2/" or "list-domain/4/list/2/" . so basically list/2/ but before this string can be anything too. how can i add that regex to above? – stackuser Feb 04 '22 at 11:16
  • 2
    You can remove the `map` and make it: `arr.some(a => new RegExp(...).test(a))` – adiga Feb 04 '22 at 11:21
  • @stackuser, just upgrade your regex expression as you wish: `arr.map(a=> new RegExp(/^(list-domain|some-group|list)\/[0-9]+(\/list\/[0-9]+)?\/$/g).test(a))` – RaiBnod Feb 04 '22 at 11:33
  • i dont want to return true for strings "list-domain/2/" , "some-group/3/", so i have rewritten the regex like so, arr.map(a=> new RegExp(/^(+\/item\/[0-9]+\/$/g).test(a)).some(value => value === true); but this gives error i think i wrote the regex wrong. – stackuser Feb 04 '22 at 11:35
  • `arr1.some(a=> new RegExp(/\/item\/[0-9]+\/$/g).test(a));` this might be what you want to have. – RaiBnod Feb 04 '22 at 11:46
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.