{"record":{"id":"279a49068f972497","repo":"grafana/k6","slug":"matching-q-against-pattern-q-w","errorCode":null,"errorMessage":"matching %q against pattern %q: %w","messagePattern":"matching %q against pattern %q: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/helpers.go","lineNumber":270,"sourceCode":"type RegExMatcher func(pattern, str string) (bool, error)\n\n// newPatternMatcher returns a [patternMatcherFunc] that uses string matching for\n// quoted strings, and ECMAScript (Sobek) regex matching for others. If the pattern\n// is empty or a single quote, it matches any string.\nfunc newPatternMatcher(pattern string, rm RegExMatcher) (patternMatcherFunc, error) {\n\tif pattern == \"\" || pattern == \"''\" {\n\t\treturn func(s string) (bool, error) { return true, nil }, nil\n\t}\n\tif isQuotedText(pattern) {\n\t\treturn func(s string) (bool, error) { return \"'\"+s+\"'\" == pattern, nil }, nil\n\t}\n\tif rm == nil {\n\t\treturn nil, fmt.Errorf(\"regex matcher must be provided\")\n\t}\n\treturn func(s string) (bool, error) {\n\t\tok, err := rm(pattern, s)\n\t\tif err != nil {\n\t\t\treturn false, fmt.Errorf(\"matching %q against pattern %q: %w\", s, pattern, err)\n\t\t}\n\t\treturn ok, nil\n\t}, nil\n}\n\n// Match evaluates the supplied string against the given pattern using the provided\n// [RegExMatcher] for regex patterns. The matcher behavior is determined by [newPatternMatcher].\nfunc (rm RegExMatcher) Match(pattern, s string) (bool, error) {\n\tm, err := newPatternMatcher(pattern, rm)\n\tif err != nil {\n\t\treturn false, fmt.Errorf(\"matching %q against pattern %q: %w\", s, pattern, err)\n\t}\n\treturn m(s)\n}\n\nvar sourceURLRegex = regexp.MustCompile(`(?s)[\\040\\t]*//[@#] sourceURL=\\s*(\\S*?)\\s*$`)\n\nfunc hasSourceURL(js string) bool {","sourceCodeStart":252,"sourceCodeEnd":288,"githubUrl":"https://github.com/grafana/k6/blob/01ffac6f245854c1b8adc6a69857c76a15f90022/internal/js/modules/k6/browser/common/helpers.go#L252-L288","documentation":"Wrap of a regex-matching failure inside the pattern matcher used for console-message and popup filtering: when a pattern is not quoted text, it is compiled and applied as an ECMAScript regex via the RegExMatcher callback, and the wrapped error reports that evaluating the pattern %q against the candidate string %q threw — typically because the pattern is an invalid regex (unbalanced parentheses, bad quantifiers) that failed at match time.","triggerScenarios":"Thrown at internal/js/modules/k6/browser/common/helpers.go:270 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Fix or simplify the regex pattern used in APIs like page.waitForConsoleMessage or popup filtering","Quote the pattern (e.g., \"'exact text'\") to use plain string matching instead of regex","Test the pattern in a JS REPL to confirm it compiles before using it in the script"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"01ffac6f245854c1b8adc6a69857c76a15f90022","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}