new ~ operator in search for regex

This commit is contained in:
zadam 2022-05-11 23:06:14 +02:00
parent bf49648896
commit 45edef2d71
3 changed files with 25 additions and 3 deletions

View File

@ -8,7 +8,17 @@ const protectedSessionService = require('../../protected_session');
const striptags = require('striptags');
const utils = require("../../utils");
const ALLOWED_OPERATORS = ['*=*', '=', '*=', '=*'];
const ALLOWED_OPERATORS = ['*=*', '=', '*=', '=*', '~'];
const cachedRegexes = {};
function getRegex(str) {
if (!(str in cachedRegexes)) {
cachedRegexes[str] = new RegExp(str, 'ms'); // multiline, dot-all
}
return cachedRegexes[str];
}
class NoteContentFulltextExp extends Expression {
constructor(operator, {tokens, raw, flatText}) {
@ -57,7 +67,8 @@ class NoteContentFulltextExp extends Expression {
if ((this.operator === '=' && token === content)
|| (this.operator === '*=' && content.endsWith(token))
|| (this.operator === '=*' && content.startsWith(token))) {
|| (this.operator === '=*' && content.startsWith(token))
|| (this.operator === '~' && getRegex(token).test(content))) {
resultNoteSet.add(becca.notes[noteId]);
}

View File

@ -1,3 +1,13 @@
const cachedRegexes = {};
function getRegex(str) {
if (!(str in cachedRegexes)) {
cachedRegexes[str] = new RegExp(str);
}
return cachedRegexes[str];
}
const stringComparators = {
"=": comparedValue => (val => val === comparedValue),
"!=": comparedValue => (val => val !== comparedValue),
@ -8,6 +18,7 @@ const stringComparators = {
"*=": comparedValue => (val => val && val.endsWith(comparedValue)),
"=*": comparedValue => (val => val && val.startsWith(comparedValue)),
"*=*": comparedValue => (val => val && val.includes(comparedValue)),
"~": comparedValue => (val => val && !!getRegex(comparedValue).test(val)),
};
const numericComparators = {

View File

@ -40,7 +40,7 @@ function getFulltext(tokens, searchContext) {
}
function isOperator(str) {
return str.match(/^[!=<>*]+$/);
return str.match(/^[!=<>*~]+$/);
}
function getExpression(tokens, searchContext, level = 0) {