Catch errors when search term is an invalid regular expression
On error search escape special characters, with only '.' matching any characters.
This commit is contained in:
@@ -1,4 +1,8 @@
|
|||||||
$(function () {
|
$(function () {
|
||||||
|
|
||||||
|
// Allow user configuration?
|
||||||
|
const allowRegex = true;
|
||||||
|
|
||||||
// Search Items
|
// Search Items
|
||||||
$('#include_modules').change(function (e) {
|
$('#include_modules').change(function (e) {
|
||||||
console.log('change');
|
console.log('change');
|
||||||
@@ -9,6 +13,29 @@ $(function () {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const reNotCache = {};
|
||||||
|
function escapeRegexp(s, not) {
|
||||||
|
if (not) {
|
||||||
|
let re = reNotCache[not];
|
||||||
|
if (!re) {
|
||||||
|
const characters = '-\/\\^$*+?.()|[\]{}'.replace(new RegExp('[' + escapeRegexp(not) + ']', 'g'), '');
|
||||||
|
re = reNotCache[not] = new RegExp('[' + escapeRegexp(characters) + ']', 'g');
|
||||||
|
}
|
||||||
|
return s.replace(re, '\\$&');
|
||||||
|
}
|
||||||
|
return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
||||||
|
}
|
||||||
|
|
||||||
|
function constructRegex(searchTerm, makeRe, allowRegex) {
|
||||||
|
try {
|
||||||
|
if (allowRegex) {
|
||||||
|
return makeRe(searchTerm);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
}
|
||||||
|
return makeRe(escapeRegexp(searchTerm, '.'));
|
||||||
|
}
|
||||||
|
|
||||||
var getSearchWeight = function (searchTerm, $matchedItem) {
|
var getSearchWeight = function (searchTerm, $matchedItem) {
|
||||||
let weight = 0;
|
let weight = 0;
|
||||||
// We could get smarter on the weight here
|
// We could get smarter on the weight here
|
||||||
@@ -46,7 +73,9 @@ $(function () {
|
|||||||
var $el = $('.navigation');
|
var $el = $('.navigation');
|
||||||
|
|
||||||
if (searchTerm.length > 1) {
|
if (searchTerm.length > 1) {
|
||||||
var regexp = new RegExp(searchTerm, 'i');
|
const regexp = constructRegex(searchTerm, function (searchTerm) {
|
||||||
|
return new RegExp(searchTerm, 'i');
|
||||||
|
}, allowRegex);
|
||||||
$el.find('li, .member-list').hide();
|
$el.find('li, .member-list').hide();
|
||||||
|
|
||||||
$el.find('li').each(function (i, v) {
|
$el.find('li').each(function (i, v) {
|
||||||
|
|||||||
Reference in New Issue
Block a user