I got another questions regarding this. :S The script is working perfectly, however, some of the “Expression” fields have different alternatives marked with a / (like abrir/cerrar los ojos).
I guess extracting this from the field to format the other field might be too complicated (especially since the alternative can be anywhere and consist of any number of words), but that is not a problem. I can live with these sentences not highlighting the expression, the functionality of the card remains.
However, the script is not returning the unformatted sentence when there is a slash in the Expression. How could I make it so that even in that case, the sentence appears? Otherwise the place where it should be remains blank and the card becomes pointless for the user.
The script is indeed a bit simplistic, and is not well equipped to deal with special characters (.^$*+?|[]()\).
This one seems to fare better:
<script>
var original_sentence = document.getElementById("sentence_input").innerHTML;
var regexText = String.raw`{{text:Expression}}`;
var formatted_sentence = original_sentence.split(regexText).join('<span class="styled-text">' + regexText + '</span>');
document.getElementById("sentence_output").innerHTML = formatted_sentence;
</script>
The only exception, hopefully quite rare, is that it does not work correctly when the fields end with an odd number of backslashes
(e.g. abrir/cerrar los ojos \, or abrir/cerrar los ojos \\\. abrir/cerrar los ojos \\ should instead work fine)