grafana/k6 · error
matching %q against pattern %q: %w
Error message
matching %q against pattern %q: %w
What it means
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.
Source
Thrown at internal/js/modules/k6/browser/common/helpers.go:270
type RegExMatcher func(pattern, str string) (bool, error)
// newPatternMatcher returns a [patternMatcherFunc] that uses string matching for
// quoted strings, and ECMAScript (Sobek) regex matching for others. If the pattern
// is empty or a single quote, it matches any string.
func newPatternMatcher(pattern string, rm RegExMatcher) (patternMatcherFunc, error) {
if pattern == "" || pattern == "''" {
return func(s string) (bool, error) { return true, nil }, nil
}
if isQuotedText(pattern) {
return func(s string) (bool, error) { return "'"+s+"'" == pattern, nil }, nil
}
if rm == nil {
return nil, fmt.Errorf("regex matcher must be provided")
}
return func(s string) (bool, error) {
ok, err := rm(pattern, s)
if err != nil {
return false, fmt.Errorf("matching %q against pattern %q: %w", s, pattern, err)
}
return ok, nil
}, nil
}
// Match evaluates the supplied string against the given pattern using the provided
// [RegExMatcher] for regex patterns. The matcher behavior is determined by [newPatternMatcher].
func (rm RegExMatcher) Match(pattern, s string) (bool, error) {
m, err := newPatternMatcher(pattern, rm)
if err != nil {
return false, fmt.Errorf("matching %q against pattern %q: %w", s, pattern, err)
}
return m(s)
}
var sourceURLRegex = regexp.MustCompile(`(?s)[\040\t]*//[@#] sourceURL=\s*(\S*?)\s*$`)
func hasSourceURL(js string) bool {View on GitHub (pinned to 93accf6570)
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
Example fix
// before
await page.waitForNavigation({ url: 'https://host/items(1)' });
// after
await page.waitForNavigation({ url: /https:\/\/host\/items\(1\)/ }); Defensive patterns
Strategy: validation
Validate before calling
function validPattern(p) {
try { new RegExp(p); return p; }
catch { throw new Error(`invalid pattern: ${p}`); }
}
await page.waitForNavigation({ url: validPattern(rawUrl) }); Prevention
- Test patterns with new RegExp before passing them
- Escape metacharacters meant literally
- Prefer exact strings or /regex/ literals over interpolated URLs
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- parsing URL pattern: %w
- waiting for URL %q: %w
- predicate function is not callable
- "handler" argument cannot be nil
- clip area is either empty or outside the viewport
AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15).
Data as JSON: /api/errors/279a49068f972497.
Report an issue: GitHub.