JavaScript 함수 highlight()

by JM note

HTML

<div id="inputText">
Hello World hello
</div>

<script>
function highlight(obj, needle) {
    var re = new RegExp(needle, 'gi');
    obj.innerHTML = obj.innerHTML.replace(re, function(str) {return '<span style="background:yellow">'+str+'</span>'});
}
highlight(document.getElementById("inputText"), "he");
</script>