-1
我有以下的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);
}
如何修改它,這樣它標誌着所有這些詞,只有在他們以@
的號碼開頭的情況下?例如,如果你改變
exp = new RegExp('\\b(' + word + ')\\b', 'gi');
到
exp = new RegExp('@\\b(' + word + ')\\b', 'gi');
它會匹配@politie
或@foo bar
內容應該匹配@foo bar
等
那是你打算把'@'放在哪裏?請記住'\'在字符串文字中有它自己的含義。 – 2013-05-09 13:23:04
不工作.... – enb081 2013-05-09 13:23:15
@squint這是一個錯字,現在修復! – 2013-05-09 13:23:46