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

168
Views
How do I ignore certain characters when replacing certain words in javascript?

I want the second window to display the sentence "I like apple and banana, and orange and maybe even pears?" with also "Banana" and "pears" highlighted in red, that is ignoring the fact that the comma or question mark follows the words. Is there any way to do this without adding items to the array such as "banana," or "pears?"

const heroes = [" banana ", " pears ", " apple ", " orange "];

var regexFromMyArray = new RegExp(heroes.join("|"), 'ig');

$('#board').keyup(function(event){
  document.getElementById('dummy').innerHTML = $('#board').html().replace(regexFromMyArray,function(str){
    return '<span class="highlighted">'+str+'</span>'
  })
})

var target = $("#dummy");
$("#board").scroll(function() {
  target.prop("scrollTop", this.scrollTop)
    .prop("scrollLeft", this.scrollLeft);
});
.color-red{
    color: #F00;
}

.original {
   position:static;
    width: 70%;
    margin: 0 auto;
    padding: 1em;
    background: #fff;
    color: #000;
    height:100px;
    margin:2px;
    border:1px solid black;
    color:#fff;
    overflow:auto;
}

#dummy{
  color:black;
}
#board{
  z-index:11;
  background:transparent;
   color:black;
    caret-color: black;
}
.original span.highlighted {
    color:red;
}

#kommentarer{
  z-index:11;
  background:transparent;
   color:black;
    caret-color: black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<h3>Search text for fruits</h3>

<br>

<p>I want the second window to display the sentence "I like apple and banana, and orange and maybe even pears?" with "Banana" and "pears" highlighted in red, that is ignoring the fact that the comma or question mark follow the words</p></p>

<br>
<br>

<p>Paste in the sentence here:</p>
<div id="board" class="original" contenteditable="true">

</div>

<br>
<br>

<p>The fruits are displayed in red:</p>
<div id="dummy" class="original">

</div>
<br>
<br>


</div>

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Two things to change:

  • Remove the spaces around the search words
  • Add word-breaks in the regex pattern: \b

So:

const heroes = ["banana", "pears", "apple", "orange"];

var regexFromMyArray = new RegExp("\\b(" + heroes.join("|") + ")\\b", 'ig');
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!