regex - Javascript mark substrings onchange event -
i trying highlight substrings of large string. following code highlights if substring first letter of large string. how can fix this? in advance
function search() { var text = document.getelementbyid("searchtext").value; if (!text) return; var regex = new regexp(text, 'gi'); var str = document.getelementbyid("input").innerhtml; var n=str.match(regex); document.getelementbyid("input").innerhtml = document.getelementbyid("input").innerhtml.replace(str, '<span style="background-color:clear;">'+str+'</span>'); // clear document.getelementbyid("input").innerhtml = document.getelementbyid("input").innerhtml.replace(n, '<span style="background-color:yellow;">'+n+'</span>'); }
try this:-
demo
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>'); }
Comments
Post a Comment