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

228
Vistas
How to properly locate all the keywords in a string that may have overlapped to each other

How to properly locate all the keywords in a string that may have overlapped to each other.

I have a sentence:

“Play around with the code and click on the Run button to view the result.”

And two keyword sets.

keywordSet1 = ['click on the Run', 'view the result'] 
keywordSet2 = ['on the', 'code and click on', 'the Run button to', 'Play around']

Apparently,

'click on the Run' in set 1 is partially overlapped with 'code and click on', 'the Run button to' in set 2;

'on the' in set 2 is overlapped with 'click on the Run' in set 1

To locate all keywords position, I write this code. To surround all keywords with by $#k1{keyword}$ or $#k2{keyword}$

text = “Play around with the code and click on the Run button to view the result.”
keywordSet1.forEach((key) => {text = text.replace(key,`$#k1$&$`)})
keywordSet2.forEach((key) => {text = text.replace(key,`$#k2$&$`)})

Problem:

However, this way sometimes will ease the marking of k1 in the sentences, which causing a problem in rendering. For example, from the result of the replace method. It became this

'$#k2Play around$ with the code and $#k1click $#k2on the$ Run$ button to $#k1view the result$'

For example, in keyword set 1, 'click on the Run' is somehow overlapped with 'code and click on' , 'the Run button to' in keyword set 2.

But with my code, it somehow didnt work as my expected, highlighted part show the problem

'$#k2Play around$ with the code and $#k1click $#k2on the$ Run$ button to $#k1view the result$' ('on the' is only highlighted by $#k2on the$)

My expectation is:

'$#k2Play around$ with the $#k2code and$ $#k1click$ $k1#$#k2on the Run$$ $#k2button to$ $#k1view the result$'

To view it visually, here is an digram

enter image description here

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