Implement "contains" and "regex" expressions highlighting in search

This commit is contained in:
Bofh 2022-12-15 13:24:27 +01:00
parent 3e1f3d01e0
commit 9858629c2b
2 changed files with 27 additions and 4 deletions

View File

@ -131,6 +131,14 @@ function remove_accents(str) {
return str.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
}
function insert_string(main_string, ins_string, pos) {
if (typeof(pos) == undefined)
pos = 0;
if (typeof(ins_string) == undefined)
ins_string = '';
return main_string.slice(0, pos) + ins_string + main_string.slice(pos);
}
function normalize_for_search(str) {
if (str.trim() === '') return '';
str = str.trim().replaceAll(/\s\s+/g, ' ');

View File

@ -75,10 +75,25 @@ window.view.instance = {
case 'has':
case 'includes':
case 'contains':
// TODO: implement contains "sr"
break;
case 'regex':
// TODO: implement regex "sr"
var t = remove_accents(html);
t = t.replace(/[^a-zA-Z0-9]/g, ' ');
t = t.toLowerCase();
content = content.replace(/\s+/g, ' ')
.trim().replaceAll(' ', '.');
content = content.replace(/\w/g,
function(m) { return m+'+?'});
var n = 0;
console.log(content);
t.replace(new RegExp(content), function(m) {
var st = t.indexOf(m, n);
var en = st + m.length;
const inj_st = '<span class="sr">';
const inj_en = '</span>';
html = insert_string(html, inj_st, st);
html = insert_string(html, inj_en, en + inj_st.length);
n = t.indexOf(m) + m.length;
});
break;
}
}