{"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/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/helpers.go#L252-L288","documentation":"URL/text matching helpers (used by k6 browser APIs that match values against patterns) build a matcher in newPatternMatcher: empty patterns match everything, quoted strings match literally, and everything else is handed to the RegExMatcher. This error is returned from inside the returned matcher closure when rm(pattern, s) fails, i.e. the regex engine rejected or failed to evaluate the pattern against the string.","triggerScenarios":"Passing a URL or text pattern to a matching API (waitForNavigation, waitForRequest-style options, expect text matching) where the pattern, after glob-to-regex treatment, is an invalid regular expression: unbalanced '(' '[' '{', stray '*', or other metacharacters the conversion does not neutralize.","commonSituations":"Using full URLs containing parentheses/brackets (e.g. 'https://host/item(1)' or '/api/v2/users[0]') as patterns; pasting user-visible URLs into waitFor calls; patterns with regex metacharacters intended literally.","solutions":["Use an exact plain string when you want exact matching, and pre-compile risky patterns with new RegExp(...) in your script to validate them","Escape regex metacharacters ( ) [ ] { } * + ? . ^ $ | \\ when they are meant literally","Replace dynamic URL segments with .* instead of raw user input","Reproduce quickly: newPatternMatcher's regex path is exercised whenever the pattern is not empty and not quoted"],"exampleFix":"// before\nawait page.waitForNavigation({ url: 'https://host/items(1)' });\n\n// after\nawait page.waitForNavigation({ url: /https:\\/\\/host\\/items\\(1\\)/ });","handlingStrategy":"validation","validationCode":"function validPattern(p) {\n  try { new RegExp(p); return p; }\n  catch { throw new Error(`invalid pattern: ${p}`); }\n}\nawait page.waitForNavigation({ url: validPattern(rawUrl) });","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Test patterns with new RegExp before passing them","Escape metacharacters meant literally","Prefer exact strings or /regex/ literals over interpolated URLs"],"tags":["browser","regex","pattern-matching","url","validation"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}