From fdedd9ddb201893ffa7724b3f9e3bf6f5174e950 Mon Sep 17 00:00:00 2001 From: eson <474420502@qq.com> Date: Sun, 23 Dec 2018 05:38:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E6=AD=A3=E5=88=99=E7=9A=84?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gjson.go | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/gjson.go b/gjson.go index 36e858f..0c316df 100644 --- a/gjson.go +++ b/gjson.go @@ -1080,6 +1080,22 @@ func parseObject(c *parseContext, i int, path string) (int, bool) { } return i, false } + +func escapeRegexpString(rpv string) string { + var content []byte + lrpv := len(rpv) + for i := 0; i < lrpv; i++ { + rpvChar := rpv[i] + if rpvChar == '\\' { + content = append(content, rpv[i+1]) + i++ + } else { + content = append(content, rpvChar) + } + } + return string(content) +} + func queryMatches(rp *arrayPathResult, value Result) bool { rpv := rp.query.value if len(rpv) > 2 && rpv[0] == '"' && rpv[len(rpv)-1] == '"' { @@ -1102,14 +1118,12 @@ func queryMatches(rp *arrayPathResult, value Result) bool { return value.Str >= rpv case "%": if rp.re == nil { - rpv = strings.Replace(rpv, `\"`, `"`, -1) - rp.re = regexp.MustCompile(rpv) + rp.re = regexp.MustCompile(escapeRegexpString(rpv)) } return rp.re.MatchString(value.Str) case "!%": if rp.re == nil { - rpv = strings.Replace(rpv, `\"`, `"`, -1) - rp.re = regexp.MustCompile(rpv) + rp.re = regexp.MustCompile(escapeRegexpString(rpv)) } return !rp.re.MatchString(value.Str) }