我正在寻找一些改进,特别是在正则表达式中,我突出显示字符串中的特定单词。情况如下:
var sentence = "J'eusse été amélioré sans des aigÜs si hauts améliorés et raisonnable. Amélioration réaméliorée";
var word = ['eusse','ameliore','aigu','aimerais']; //Matched keywords extracted from database where there are stored without diacritrics
var new_s = sentence, match, output, nb = 0, index = [];
var new_s_cache = removeDiacritics(sentence).replace("'"," ");
for (var i=0;i<word.length;i++){// For each matched word it stores its position and length in the array called index
var wordInput = word[i].substr(0,4);
var re = new RegExp("(?:^|\\W)"+wordInput+"(\\w*)(?!\\w)","gi");
while (match = re.exec(new_s_cache)) {
index.push([match.index,match[0].length]);
}
}
index.sort(); // Sort index so that it highlights word in the right order
for (var j=0;j<index.length;j++) { //It hightlights the word in the orginal sentence with diatrics
new_s = new_s.substr(0,index[j][0]+1+nb) + "<b>" + new_s.substr(index[j][0]+1+nb,index[j][1]) + "</b>" + new_s.substr(index[j][0]+index[j][1]+1+nb);
nb = nb + 7; // take into account the "<b></b>" length
}
output = new_s ;
我在哪里寻找改进措施?
这是JSfiddle:https://jsfiddle.net/qqabomsg/3/
提前致谢!