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:
Maximilian Krög
2020-02-09 17:53:22 +01:00
parent d56513b722
commit 2b8582fcad

View File

@@ -1,4 +1,8 @@
$(function () {
// Allow user configuration?
const allowRegex = true;
// Search Items
$('#include_modules').change(function (e) {
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) {
let weight = 0;
// We could get smarter on the weight here
@@ -46,7 +73,9 @@ $(function () {
var $el = $('.navigation');
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').each(function (i, v) {