Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

267
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda