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, ""); 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) { function normalize_for_search(str) {
if (str.trim() === '') return ''; if (str.trim() === '') return '';
str = str.trim().replaceAll(/\s\s+/g, ' '); str = str.trim().replaceAll(/\s\s+/g, ' ');
@ -140,7 +148,7 @@ function normalize_for_search(str) {
var word = words[i]; var word = words[i];
word = remove_accents(word); word = remove_accents(word);
word = word.toLowerCase(); word = word.toLowerCase();
word = word.replaceAll(/[^a-z0-9]+/g, ''); word = word.replaceAll(/[^a-z0-9]+/g, ' ');
if (word.trim() === '') if (word.trim() === '')
continue; continue;
word = word.replaceAll('4','a'); word = word.replaceAll('4','a');

View File

@ -75,10 +75,25 @@ window.view.instance = {
case 'has': case 'has':
case 'includes': case 'includes':
case 'contains': case 'contains':
// TODO: implement contains "sr"
break;
case 'regex': 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; break;
} }
} }