Skip to content

Commit 6aa81fe

Browse files
author
gmsoft
committed
修复搜索关键字中包含特殊字符引起的异常
转义所有的正则表达式字符
1 parent 9aba087 commit 6aa81fe

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

component.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "0.33.0",
2+
"version": "0.33.1",
33
"description": "搜索页面关键字高亮显示",
44
"name": "jquery-search-highlight",
55
"main": "jquery.searchHighlight.js",

jquery.searchHighlight.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898

9999
return query;
100100
},
101-
regexAccent : [
101+
regexAccent : [
102102
[/[\xC0-\xC5\u0100-\u0105]/ig,'a'],
103103
[/[\xC7\u0106-\u010D]/ig,'c'],
104104
[/[\xC8-\xCB]/ig,'e'],
@@ -112,20 +112,24 @@
112112
[/[\x91\x92\u2018\u2019]/ig,'\'']
113113
],
114114
matchAccent : /[\x91\x92\xC0-\xC5\xC7-\xCF\xD1-\xD6\xD8-\xDC\xFF\u0100-\u010D\u015A-\u0167\u2018\u2019]/ig,
115-
replaceAccent: function(q) {
116-
SearchHighlight.matchAccent.lastIndex = 0;
115+
replaceAccent: function(q) {
116+
SearchHighlight.matchAccent.lastIndex = 0;
117117
if(SearchHighlight.matchAccent.test(q)) {
118118
for(var i=0,l=SearchHighlight.regexAccent.length;i<l;i++)
119119
q = q.replace(SearchHighlight.regexAccent[i][0],SearchHighlight.regexAccent[i][1]);
120120
}
121121
return q;
122122
},
123123
escapeRegEx : /((?:\\{2})*)([[\]{}*?|])/g, //the special chars . and + are already gone at this point because they are considered split chars
124+
escapeAllRegEx : /[\(\)\[\]\?\.\+\*\^\$]/g,
124125
buildReplaceTools : function(query) {
125126
var re = [], regex;
126127
$.each(query,function(i,n){
127-
if(n = SearchHighlight.replaceAccent(n).replace(SearchHighlight.escapeRegEx,"$1\\$2"))
128+
if(n = SearchHighlight.replaceAccent(n).replace(SearchHighlight.escapeRegEx,"$1\\$2")){
129+
SearchHighlight.escapeAllRegEx.lastIndex = 0;
130+
n = n.replace(SearchHighlight.escapeAllRegEx, "\\$&");
128131
re.push(n);
132+
}
129133
});
130134

131135
regex = re.join("|");
@@ -161,14 +165,14 @@
161165
for(var startIndex=0,endIndex=el.childNodes.length;startIndex<endIndex;startIndex++) {
162166
var item = el.childNodes[startIndex];
163167
if ( item.nodeType != 8 ) {//comment node
164-
//text node
168+
//text node
165169
if(item.nodeType==3) {
166170
var text = item.data, textNoAcc = SearchHighlight.replaceAccent(text);
167171
var newtext="",match,index=0;
168172
SearchHighlight.regex.lastIndex = 0;
169173
while(match = SearchHighlight.regex.exec(textNoAcc)) {
170174
newtext += text.substr(index,match.index-index)+'<span class="'+
171-
SearchHighlight.subs[match[matchIndex].toLowerCase()]+'">'+text.substr(match.index,match[0].length)+"</span>";
175+
(SearchHighlight.subs[match[matchIndex].toLowerCase()]||SearchHighlight.options.style_name)+'">'+text.substr(match.index,match[0].length)+"</span>";
172176
index = match.index+match[0].length;
173177
}
174178
if(newtext) {
@@ -182,7 +186,7 @@
182186
} else {
183187
if(item.nodeType==1 && item.nodeName.search(SearchHighlight.nosearch)==-1)
184188
SearchHighlight.hiliteTree(item,query,noHighlight);
185-
}
189+
}
186190
}
187191
}
188192
}

0 commit comments

Comments
 (0)