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

230
Views
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
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!