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

266
Views
How to check if a particular string exists using regex and javascript?

i want to check if a particular string exists in an array of strings. i have an array of strings like below,

const arr = [
    "item-group/1/",
    "item/1/component/2/product/3",
    "item-group/1/item/1/product/5",
    "item/1",

    "item/2",
    "item/2/component/3/products/6",
    "item/2/products/7",
]

i want to check if there is strings matching item/<respective_number> say "item/2" or "item/3" for the products strings with respective item number.

these strings have a meaning

"item-group/1/", //here we have item-group with id 1
    "item/1/component/2/product/3", //here product with id 3 belongs to item with id 
    //1
    "item-group/1/item/1/product/5", //product with id 5 belongs to item with id 1
    "item/1", //item with id 1

     "item/2", //item with id 2
    "item/2/component/3/products/6",//product with id 6 belongs to item with id 2
    "item/2/products/7", //product with id 7 belongs to item with item with id 2

so for these strings i want to check if there is products. if there is strings with products then i want to check if the item they belong to have a string with only item/1 . in other words number after item/ can be anything.

so based on goal above, i filter the strings that contains products/ so i do like below,

const products = arr.filter(a =>
  new RegExp(/products\/([0-9]+)(\/)?$/g).test(a)
);

this will return the strings like below,

"item/1/component/2/product/3",
    "item-group/1/item/1/product/5",

    "item/2/component/3/products/6",
    "item/2/products/7",

now i get the number after item/ with below code

const rex = /\item\/(\d+)\//;
const itemIds = products.map(str => (str.match(rex) ?? [])[1])
  .filter(result => result !== undefined);
     

now the output in itemIds is [1,1,2,2] i remove duplicate values like so

const newItemIds = new Set(itemIds);

now how do i check if arr has strings item/<number_matching_item_id>

so it should look for item/1 and item/2 nothing should follow after these strings.

how can i do that could someone help me with this. thanks.

about 4 years ago · Juan Pablo Isaza
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!