diff --git a/files/helpers/regex.py b/files/helpers/regex.py index 3c32b3a52f..ed6d841cbb 100644 --- a/files/helpers/regex.py +++ b/files/helpers/regex.py @@ -173,6 +173,9 @@ video_link_regex = re.compile(f"https://(videos\.)?{SITE}\/(videos\/)?[0-9a-zA-Z asset_image_link_regex = re.compile(f"https:\/\/(i\.)?{SITE}\/assets\/images\/[\w\/]+.webp(\?x=\d+)?", flags=re.A) +search_regex_1 = re.compile(r'[\0():|&*!<>]', flags=re.A) +search_regex_2 = re.compile(r"'", flags=re.A) +search_regex_3 = re.compile(r'\s+', flags=re.A) ###REDDIT diff --git a/files/routes/search.py b/files/routes/search.py index 74807e3fcb..32e19f83ef 100644 --- a/files/routes/search.py +++ b/files/routes/search.py @@ -227,11 +227,11 @@ def searchcomments(v): else: comments = comments.filter(Comment.author_id == author.id) if 'q' in criteria: - tokens = map(lambda x: re.sub(r'[\0():|&*!<>]', '', x), criteria['q']) + tokens = map(lambda x: search_regex_1.sub('', x), criteria['q']) tokens = filter(lambda x: len(x) > 0, tokens) - tokens = map(lambda x: re.sub(r"'", "\\'", x), tokens) + tokens = map(lambda x: search_regex_2.sub("\\'", x), tokens) tokens = map(lambda x: x.strip(), tokens) - tokens = map(lambda x: re.sub(r'\s+', ' <-> ', x), tokens) + tokens = map(lambda x: search_regex_3.sub(' <-> ', x), tokens) comments = comments.filter(Comment.body_ts.match( ' & '.join(tokens), postgresql_regconfig='english')) @@ -328,11 +328,11 @@ def searchmessages(v): else: comments = comments.filter(Comment.author_id == author.id) if 'q' in criteria: - tokens = map(lambda x: re.sub(r'[\0():|&*!<>]', '', x), criteria['q']) + tokens = map(lambda x: search_regex_1.sub('', x), criteria['q']) tokens = filter(lambda x: len(x) > 0, tokens) - tokens = map(lambda x: re.sub(r"'", "\\'", x), tokens) + tokens = map(lambda x: search_regex_2.sub("\\'", x), tokens) tokens = map(lambda x: x.strip(), tokens) - tokens = map(lambda x: re.sub(r'\s+', ' <-> ', x), tokens) + tokens = map(lambda x: search_regex_3.sub(' <-> ', x), tokens) comments = comments.filter(Comment.body_ts.match( ' & '.join(tokens), postgresql_regconfig='english'))