forked from mrlan/EnglishPal
				
			highlight.js: fix Bug 522.
							parent
							
								
									e74f1ff477
								
							
						
					
					
						commit
						417dbc22f8
					
				|  | @ -31,9 +31,19 @@ function highLight() { | ||||||
|         list[i] = list[i].replace(/(^\s*)|(\s*$)/g, ""); //消除单词两边的空字符
 |         list[i] = list[i].replace(/(^\s*)|(\s*$)/g, ""); //消除单词两边的空字符
 | ||||||
|         if (list[i] !== "" && "<mark>".indexOf(list[i]) === -1 && "</mark>".indexOf(list[i]) === -1) { |         if (list[i] !== "" && "<mark>".indexOf(list[i]) === -1 && "</mark>".indexOf(list[i]) === -1) { | ||||||
| 	    //将文章中所有出现该单词word的地方改为:" <mark>" + word + "<mark> "。 正则表达式RegExp()中,"\\s"代表单词前后必须要有空格,以防止只对单词中的部分字符高亮的情况出现。
 | 	    //将文章中所有出现该单词word的地方改为:" <mark>" + word + "<mark> "。 正则表达式RegExp()中,"\\s"代表单词前后必须要有空格,以防止只对单词中的部分字符高亮的情况出现。
 | ||||||
|  |             let articleContent_fb = articleContent;  //文章副本
 | ||||||
|  |             while(articleContent_fb.toLowerCase().indexOf(list[i]) !== -1){ | ||||||
|  |                 //针对同一篇文章中可能存在相同单词的不同大小写问题,采用while循环判断副本中是否还存在匹配单词。
 | ||||||
|  | 
 | ||||||
|  |                 //找到副本中和list[i]匹配的第一个单词(第一种匹配情况),并赋值给list[i]。
 | ||||||
|  |                 list[i] = articleContent_fb.substr(articleContent_fb.toLowerCase().indexOf(list[i]),list[i].length); | ||||||
|  | 
 | ||||||
|  |                 articleContent_fb = articleContent_fb.replace(new RegExp("\\s"+list[i]+"\\s","g"),""); //删除副本中和list[i]匹配的单词
 | ||||||
|  | 
 | ||||||
|                 articleContent = articleContent.replace(new RegExp("\\s"+list[i]+"\\s", "g"), " <mark>" + list[i] + "</mark> "); |                 articleContent = articleContent.replace(new RegExp("\\s"+list[i]+"\\s", "g"), " <mark>" + list[i] + "</mark> "); | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|  |     } | ||||||
|     document.getElementById("article").innerHTML = articleContent; |     document.getElementById("article").innerHTML = articleContent; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue