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

155
Views
Exclude HTML tags from search in a string (ReactJS)

I have an array of titles which follow a similar format as below:

dolor ea consequat <span style="color: #fa8231;">&lt;deserunt&gt;</span> officia non elit <span style="color: #41B3A3;">&lt;aliqua&gt;</span> ex minim eu

which are displayed to the user using the dangerouslysetInnerHTML property.

They are displayed to the user in a list and the user is able to filter the array given keyword(s) and the keyword(s) are highlighted. For example, searching for dolor would return the following string (using replace()) which is also displayed using the dangerouslysetInnerHTML property of ReactJS:

<span style="background-color: #6d7fcc; color: #fff">dolor</span> ea consequat <span style="color: #fa8231;">&lt;deserunt&gt;</span> officia non elit <span style="color: #41B3A3;">&lt;aliqua&gt;</span> ex minim eu

NOTE: The keyword can also include the text between the &lt;(<) and &gt;(>) tags and that text should also be highlighted. And the <span> tags in the original string should not be included in the search and replace. Here's my RegEx code to search for the keyword(s) in the string:

const regexStr = `${keyword}(?=[^>]*<)`;
const highlightRegex = new RegExp(regexStr, 'gi');

The problem I am encountering is that the search does not include the last part of the string (anything past the last </span>) in the search, so ex minim eu is not included. I am trying to learn RegEx, so my knowledge is weak. Could someone tell me what I am doing incorrectly?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

UDPATE 1:

I added this to my regex and that seemed to have work. Not sure if this is the best or most efficient way. Would still love any feedback!

const regexStr = `${keyword}((?=[^>]*<)|(?![^<]*>))`; 
about 4 years ago · Juan Pablo Isaza Report
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!