How to highlight text using javascript

后端 未结 13 2240
名媛妹妹
名媛妹妹 2020-11-22 02:32

Can someone help me with a javascript function that can highlight text on a web page. And the requirement is to - highlight only once, not like highlight all occurrences of

13条回答
  •  醉话见心
    2020-11-22 03:01

    Here's my regexp pure JavaScript solution:

    function highlight(text) {
        document.body.innerHTML = document.body.innerHTML.replace(
            new RegExp(text + '(?!([^<]+)?<)', 'gi'),
            '$&'
        );
    }
    

提交回复
热议问题