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
javascript word in sentence matching

I'm currently trying to find matching words within sentences, and I have a method that mostly works. It's working on a HTMLNode.textContent where I currently use the indexOf method to find a match.

let exists = node.textContent.indexOf(x.searchText) > -1

I need it to match exactly, including any spaces and special characters in the word, which works with indexOf, but I also want to be able to create exceptions to this exact search.

For example the word "Burnett’s" should be equal to "Burnett's" despite the apostrophe formats being different.

There are quite a lot of checks when this runs, so preferably I want something fast without having to check every word if it contains either of those apostrophes and if so remove it so it's not part of the comparison. Are there any better ways to check for or do it on?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You could replace every exception variant to a single one to check against:

function cleanText(str) {
    return str.replace(/[`’]/g,"'");
}
            
let exists = cleanText(node.textContent).indexOf(cleanText(x.searchText)) > -1;

You can easily chain additional exceptions in the cleanText function, for example:

function cleanText(str) {
    return str.replace(/[`’]/g,"'").replace(/@/g, " at ").replace(/&/g, " and ");
}
about 4 years ago · Santiago Trujillo 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!