Implement "contains" and "regex" expressions highlighting in search
This commit is contained in:
parent
3e1f3d01e0
commit
9858629c2b
|
@ -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, ' ');
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue