Request: javascript replace

If i have field “sentence” = gefürchteter
and field “word” = gefürchtet[fürchten,v,]
and i want the sentence to show like that: gefürchteter , to bold the word in the senentce with partial match and neglecting what’s inside [fürchten,v,].
can someone provide a script for that

<div id="word" hidden>{{Word}}</div>
<div id="sentence">{{Sentence}}</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>${word}</b>`
    );
  })();
</script>
1 Like

Thanks, but i want to partial match the word and neglecting what’s between brackets, if word = fish [verb]
sentence will show: i like fishing
i used this script and there was no bolding

Ah yes. This regular expression will terminate at the first opening bracket:

const word = document.getElementById("word").innerText.match(/^[^[]*/gs);
5 Likes

Brilliant!
Thanks, it’s very kind of you
i wish you the best

can you change innertextmatch to do this
Sentence: Ich schneide Filme.
Word: schneidet · schnitt · hat geschnitten

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.