grafana/k6 · error

matching against pattern

Error message

matching %q against pattern %q: %w

What it means

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.

Solutions

  1. Fix or simplify the regex pattern used in APIs like page.waitForConsoleMessage or popup filtering
  2. Quote the pattern (e.g., "'exact text'") to use plain string matching instead of regex
  3. Test the pattern in a JS REPL to confirm it compiles before using it in the script
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at internal/js/modules/k6/browser/common/helpers.go:270 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of grafana/k6@01ffac6f24 (2026-08-18). Data as JSON: /api/errors/279a49068f972497. Report an issue: GitHub.

Appendix: 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 01ffac6f24)