|
87 | 87 |
|
88 | 88 | var matches = [];
|
89 | 89 |
|
90 |
| - var wordColorMap = {}; |
| 90 | + var wordColorMap = {}, |
| 91 | + allWords = []; |
91 | 92 | $.each(this.settings.words, function (color, words) {
|
92 | 93 | $.each(words, function (index, word) {
|
93 | 94 | wordColorMap[word] = color;
|
| 95 | + allWords.push(word); |
94 | 96 | });
|
95 | 97 | });
|
96 | 98 |
|
97 |
| - $.each(wordColorMap, function (word, color) { |
| 99 | + var allWordsRe = allWords.map(function (word) { |
98 | 100 | var wordsRe = htmlDecode(word);
|
99 |
| - var re = that.spacer + '(' + wordsRe + ')' + that.spacer; |
100 |
| - var regex = new RegExp(re, that.regParam); |
101 |
| - |
102 |
| - var wordMatches = text.match(regex); |
103 |
| - if (wordMatches) { |
104 |
| - word = htmlDecode(word); |
105 |
| - matches.push(word); |
106 |
| - text = text.replace( |
107 |
| - new RegExp(that.spacer + word + that.spacer, that.regParam), |
108 |
| - function(innerMatch, start, contents) { |
109 |
| - var encodedMatch = innerMatch |
110 |
| - .replace(/[&"<>]/g, function (c) { |
111 |
| - return { |
112 |
| - '&': "&", |
113 |
| - '"': """, |
114 |
| - '<': "<", |
115 |
| - '>': ">" |
116 |
| - }[c]; |
117 |
| - }); |
118 |
| - |
119 |
| - return '<mark style="background-color:'+ color +';">' + encodedMatch + '</mark>'; |
120 |
| - } |
121 |
| - ); |
122 |
| - } |
123 |
| - }); |
| 101 | + var re = '(' + that.spacer + wordsRe + that.spacer + ')'; |
| 102 | + return re; |
| 103 | + }).join('|'); |
| 104 | + var regex = new RegExp(allWordsRe, that.regParam); |
| 105 | + |
| 106 | + var wordMatches = text.match(regex); |
| 107 | + if (wordMatches) { |
| 108 | + text = text.replace(regex, function(innerMatch, start, contents) { |
| 109 | + matches.push(innerMatch); |
| 110 | + var encodedMatch = innerMatch |
| 111 | + .replace(/[&"<>]/g, function (c) { |
| 112 | + return { |
| 113 | + '&': "&", |
| 114 | + '"': """, |
| 115 | + '<': "<", |
| 116 | + '>': ">" |
| 117 | + }[c]; |
| 118 | + }); |
| 119 | + |
| 120 | + var color = wordColorMap[innerMatch]; |
| 121 | + return '<mark style="background-color:'+ color +';">' + encodedMatch + '</mark>'; |
| 122 | + }); |
| 123 | + } |
124 | 124 |
|
125 | 125 | $.each(this.settings.ranges, function(i, range) {
|
126 | 126 | if (range.start < text.length) {
|
|
0 commit comments