我有以下 Javascript 函数,可以在 contenteditable div 中查找一组单词的所有匹配项:
var 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 '<strong>' + m + '</strong>';
});
});
//html = html.replace(' ', ' ').replace(/\s+/g, ' ');
div.html(html);
}
我如何修改它以便它标记所有这些单词,仅在它们以 at 符号开头的情况下@
?例如,它应该匹配@foo bar
等。