From 9bd3f1ad2e6f5e3a035ec5770b40a7a7fa63084c Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Fri, 18 Nov 2022 08:44:10 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E6=94=B9=E8=BF=9B=E8=99=9A=E6=8B=9F?= =?UTF-8?q?=E5=BC=95=E7=94=A8=E5=92=8C=E5=8F=8D=E9=93=BE=E6=8F=90=E5=8F=8A?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E5=8C=B9=E9=85=8D=20Fix=20https://github.com?= =?UTF-8?q?/siyuan-note/siyuan/issues/6600?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/search/mark.go | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/kernel/search/mark.go b/kernel/search/mark.go index e16fe6fa4..e858f3fa5 100644 --- a/kernel/search/mark.go +++ b/kernel/search/mark.go @@ -85,7 +85,7 @@ func EncloseHighlighting(text string, keywords []string, openMark, closeMark str } re := ic + "(" for i, k := range keywords { - k = keyword2regexp(k) + k = regexp.QuoteMeta(k) re += "(" + k + ")" if i < len(keywords)-1 { re += "|" @@ -98,7 +98,7 @@ func EncloseHighlighting(text string, keywords []string, openMark, closeMark str }) } else { for _, k := range keywords { - k = keyword2regexp(k) + k = regexp.QuoteMeta(k) var repls, words []string if re, err := regexp.Compile(ic + k); nil == err { words = re.FindAllString(text, -1) @@ -115,12 +115,3 @@ func EncloseHighlighting(text string, keywords []string, openMark, closeMark str } return text } - -func keyword2regexp(k string) string { - k = strings.ReplaceAll(k, "*", ".*") - k = strings.ReplaceAll(k, "?", ".") - k = strings.ReplaceAll(k, "%", ".*") - k = strings.ReplaceAll(k, "_", ".") - k = strings.ReplaceAll(k, "\\\\", "\\") - return k -}