|
|
@@ -146,20 +146,39 @@ Page({
|
|
146
|
146
|
let hl="nonelight";
|
|
147
|
147
|
if (that.data.IsShowKeyword)
|
|
148
|
148
|
hl="highlight";
|
|
|
149
|
+
|
|
|
150
|
+ // 简化的解决方案:按长度排序并使用临时标记
|
|
149
|
151
|
for(let i=0;i<content.ArticleEnglish.length;i++){
|
|
150
|
|
- for(let j=0;j<content.FormsOfWords.length;j++){
|
|
151
|
|
- let word = content.FormsOfWords[j];
|
|
|
152
|
+ // 1. 先对单词按长度从长到短排序,确保先处理"Come on"再处理"Come"
|
|
|
153
|
+ let sortedWords = [...content.FormsOfWords].sort((a, b) => b.length - a.length);
|
|
|
154
|
+
|
|
|
155
|
+ // 2. 使用临时标记替换匹配的单词,避免HTML解析问题
|
|
|
156
|
+ let text = content.ArticleEnglish[i];
|
|
|
157
|
+ let placeholders = [];
|
|
|
158
|
+ for(let j=0;j<sortedWords.length;j++){
|
|
|
159
|
+ let word = sortedWords[j];
|
|
152
|
160
|
let regex = new RegExp(`\\b${word}\\b[.,!?;:]?`, 'gi');
|
|
153
|
|
- content.ArticleEnglish[i] = content.ArticleEnglish[i].replace(regex, match => {
|
|
|
161
|
+
|
|
|
162
|
+ text = text.replace(regex, match => {
|
|
154
|
163
|
let punctuation = match.match(/[.,!?;:]$/);
|
|
155
|
164
|
let punc = punctuation ? punctuation[0] : '';
|
|
156
|
165
|
let wordPart = match.replace(/[.,!?;:]$/, '');
|
|
157
|
|
- return `<span class='`+hl+`'>${wordPart}</span>${punc}`;
|
|
|
166
|
+
|
|
|
167
|
+ // 使用唯一的占位符
|
|
|
168
|
+ let placeholder = `__PLACEHOLDER_${placeholders.length}__`;
|
|
|
169
|
+ placeholders.push(`<span class='${hl}'>${wordPart}</span>${punc}`);
|
|
|
170
|
+ return placeholder;
|
|
158
|
171
|
});
|
|
159
|
172
|
}
|
|
|
173
|
+
|
|
|
174
|
+ // 3. 将临时标记替换为实际的HTML标签
|
|
|
175
|
+ for(let j=0; j<placeholders.length; j++){
|
|
|
176
|
+ text = text.replace(`__PLACEHOLDER_${j}__`, placeholders[j]);
|
|
|
177
|
+ }
|
|
|
178
|
+
|
|
|
179
|
+ content.ArticleEnglish[i] = text;
|
|
160
|
180
|
}
|
|
161
|
181
|
content.ArticleEnglishArr=[];
|
|
162
|
|
- //debugger;
|
|
163
|
182
|
for(let i=0;i<content.ArticleEnglish.length;i++){
|
|
164
|
183
|
let obj=common.splitByMultipleDelimiters(content.ArticleEnglish[i],["<span class='"+hl+"'>","</span>"]);
|
|
165
|
184
|
obj=common.removeDuplicateAndTrimStrings(obj);
|