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

150
Views
Highlight partial matches from array of strings - JavaScript

I'm creating a small project which will go through the contents of a textarea element, find words from the banned words list and highlight them if they are found. I've implemented a solution which works for full words, however in the scenario of edge cases, I've run into quite the pickle. Below is the code from the project:

const bannedWords = ['treatment', 'cancer', 'allergens'];
document.addEventListener('DOMContentLoaded', () => {
    let checkButton = document.querySelector('.check__btn');
    let textArea = document.querySelector('textarea');

    checkButton.addEventListener('click', (e) => {
        e.preventDefault();
        let inputString = textArea.value.split(" ");
        for (let i = 0; i < inputString.length; i++) {

            if (bannedWords.includes(inputString[i])) {
                inputString[i] = `<mark>${inputString[i]}</mark>`;
            }
        }
        let outputText = document.querySelector('.output__text');
        outputText.innerHTML = inputString.join(" ");

        return false;
    });
});

So for example, if a user was to input 'treat' or 'cancerous' for example, they won't be highlighted since they technically don't appear in the list.

Any help with this would be appreciated! Cheers.

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

0

I think your problem is quite similar to what vscode files search does. When you hit Ctrl+P and enter some file name for eg: "numbrepinut" it would still show you the file named numberInput.js considering it a similar result. Please check the following links:

  1. Which algorithm is used to implement search for files in Visual Studio Code / Google Chrome Developer / Sublime (Ctrl+p or Cmd+p)?
  2. https://levelup.gitconnected.com/fuzzy-searches-in-vs-code-tips-to-increase-developer-focus-and-productivity-6809c0f3ce9a
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!