In the given code snippet, a method is provided to highlight substrings within a large string. However, there seems to be an issue with supporting a special character *
which should represent any string of any length. Ideally, inputting *
in the search text field should result in highlighting the entire loaded text file. Similarly, entering *a
should highlight all words ending with 'a'. 'a' should match all words containing the letter a
. Currently, it appears that the *
character is not being recognized at all. Any suggestions on how to address this issue would be greatly appreciated. Thank you for your assistance.
function search() {
var hid = document.getElementById('hidtxt').value;
if(hid.length == 0) hid.value=document.getElementById("input").innerHTML;
var text = document.getElementById("searchText").value;
if (!text) return;
var regex = new RegExp(text, 'gi');
document.getElementById("input").innerHTML = hid.replace(regex, '<span style="background-color:yellow;">$&</span>');
}