Sentence: Wir gehen ins Kino um 19 Uhr.
Word: Kino
I have this script to make the word kino bolded:
<div id="word" hidden>{{Word}}</div>
<script>
(() => {
const word = document.getElementById("word").innerText;
const sentenceEl = document.getElementById("sentence");
sentenceEl.innerHTML = sentenceEl.innerHTML.replace(
// case-insensitive regular expression
new RegExp(word, "gi"), `<b><i><span style=' '>${word}</span></b></i>`
);
})();
</script>
but if Word: das Kino · Kinos · Kinos
the word kino doesn’t get bolded in the sentence, so i want to edit the code
I know nothing about programming.
2
Answers
Firstly, I think that the code
const word = document.getElementById("word").innerText;
is wrong. We must uselet word = document.getElementById("word").textContent;
Secondly, the replace regex should be simple as this
Remember to close the html tag with right position.
See the demo here https://codepen.io/giang-vincent/pen/eYPgKep?editors=1111
Test unique changes