The following method highlights substrings of a large string. What I want is to support a special character *
which represents any string of any length. For example, if *
is inputted in the search text field, the whole loaded text file should be highlighted. When the input becomes *a
, all the words that end with 'a' should be highlighted. 'a' matches all the words that contain a
. However, at the moment *
character is not even picked up. What can I do to fix this problem? Thanks in advance..
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>');
}