forked from mrlan/EnglishPal
				
			黄子睿: 修复 'Otherwise,' 这种情况无法高亮的问题,即 Otherwise 后面跟了个逗号
							parent
							
								
									417dbc22f8
								
							
						
					
					
						commit
						3bce450620
					
				|  | @ -30,17 +30,22 @@ function highLight() { | |||
|     for (let i = 0; i < list.length; ++i) { | ||||
|         list[i] = list[i].replace(/(^\s*)|(\s*$)/g, ""); //消除单词两边的空字符
 | ||||
|         if (list[i] !== "" && "<mark>".indexOf(list[i]) === -1 && "</mark>".indexOf(list[i]) === -1) { | ||||
| 	    //将文章中所有出现该单词word的地方改为:" <mark>" + word + "<mark> "。 正则表达式RegExp()中,"\\s"代表单词前后必须要有空格,以防止只对单词中的部分字符高亮的情况出现。
 | ||||
|            //将文章中所有出现该单词word的地方改为:"<mark>" + word + "<mark>"。 正则表达式RegExp()中,"\\b"代表单词边界匹配。
 | ||||
| 
 | ||||
|             //修改代码
 | ||||
|             let articleContent_fb = articleContent;  //文章副本
 | ||||
|             while(articleContent_fb.toLowerCase().indexOf(list[i]) !== -1){ | ||||
|             let count = 1000;  //简单计数器,防止陷入死循环。
 | ||||
|             while(articleContent_fb.toLowerCase().indexOf(list[i].toLowerCase()) !== -1 && list[i]!=""){ | ||||
|                 //针对同一篇文章中可能存在相同单词的不同大小写问题,采用while循环判断副本中是否还存在匹配单词。
 | ||||
| 
 | ||||
|                 count--; | ||||
|                 if(count <= 0)break;   //TimeOut!
 | ||||
| 
 | ||||
|                 //找到副本中和list[i]匹配的第一个单词(第一种匹配情况),并赋值给list[i]。
 | ||||
|                 list[i] = articleContent_fb.substr(articleContent_fb.toLowerCase().indexOf(list[i]),list[i].length); | ||||
|                 list[i] = articleContent_fb.substr(articleContent_fb.toLowerCase().indexOf(list[i].toLowerCase()),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_fb = articleContent_fb.replace(list[i],""); //删除副本中和list[i]匹配的单词
 | ||||
|                 articleContent = articleContent.replace(new RegExp("\\b"+list[i]+"\\b","g"),"<mark>" + list[i] + "</mark>"); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue