Replace specific word in contenteditable

前端 未结 3 1694
我寻月下人不归
我寻月下人不归 2021-02-08 07:52

I have a contenteditable div

I need to get the last word from caret position and on certai

3条回答
  •  一生所求
    2021-02-08 08:27

     words = ['oele', 'geel', 'politie', 'foo bar'];
    
    function markWords() {
        var html = div.html().replace(/<\/?strong>/gi, ''),
            text = html.replace(/<[^>]+>/g, ' ').replace(/\s+/g, ' '),
            exp;
        $.each(words, function(i, word) {
            exp = new RegExp('\\b(' + word + ')\\b', 'gi');
            html = html.replace(exp, function(m) {
    console.log('WORD MATCH:', m);
                return '' + m + '';
            });
        });
        //html = html.replace(' ', ' ').replace(/\s+/g, ' ');
    console.log('HTML:', html);
    console.log('----');
        div.html(html);
    }
    

    Call this function on setinterval

    Fiddle

提交回复
热议问题