{"record":{"id":"43c5977e2b8462c6","repo":"grafana/k6","slug":"matching-url-with-regex-w","errorCode":null,"errorMessage":"matching url with regex: %w","messagePattern":"matching url with regex: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/browser/metric_event_mapping.go","lineNumber":19,"sourceCode":"package browser\n\nimport (\n\t\"fmt\"\n\n\t\"go.k6.io/k6/v2/internal/js/modules/k6/browser/common\"\n)\n\n// mapMetricEvent to the JS module.\nfunc mapMetricEvent(vu moduleVU, event common.PageEvent) mapping {\n\trt := vu.Runtime()\n\tem := event.Metric\n\n\treturn mapping{\n\t\t\"tag\": func(urls common.TagMatches) error {\n\t\t\tcallback := func(pattern, url string) (bool, error) {\n\t\t\t\tmatched, err := rt.RunString(pattern + `.test('` + url + `')`)\n\t\t\t\tif err != nil {\n\t\t\t\t\treturn false, fmt.Errorf(\"matching url with regex: %w\", err)\n\t\t\t\t}\n\t\t\t\treturn matched.ToBoolean(), nil\n\t\t\t}\n\n\t\t\treturn em.Tag(callback, urls) //nolint:wrapcheck\n\t\t},\n\t}\n}\n","sourceCodeStart":1,"sourceCodeEnd":28,"githubUrl":"https://github.com/grafana/k6/blob/01ffac6f245854c1b8adc6a69857c76a15f90022/internal/js/modules/k6/browser/browser/metric_event_mapping.go#L1-L28","documentation":"Thrown inside the 'metric' page event handler when k6 evaluates your URL regex: it builds the script `pattern + \".test('\" + url + \"')\"` and runs it in the Sobek runtime (browser/metric_event_mapping.go:19). Any failure of that RunString — invalid regex literal, ReferenceError from a non-regex pattern, or a URL containing a quote/backslash that breaks the string literal — is wrapped with this message. The event.tag call is what invokes it, from page.on('metric', e => e.tag({ name, matches: [{ url, method }] })).","triggerScenarios":"Passing a bare pattern without regex delimiters — e.g. url: 'api/goods' becomes `api/goods.test('...')` (ReferenceError); an invalid literal like '/goods(' (SyntaxError); or a navigated URL that contains an apostrophe or backslash, which terminates/escapes the embedded '...' string and makes the generated script unparseable.","commonSituations":"Writing url patterns as plain strings because Go-style regex habits (no delimiters) carry over; copying patterns from k6's options.name/http debug shortcuts; testing against URLs with query strings containing encoded quotes.","solutions":["Pass a complete, slash-delimited JavaScript regex literal string: url: '/api/goods/\\\\d+'.","Pretest the pattern in the same script: new RegExp(pattern.source ?? pattern) inside try/catch before calling tag.","Keep patterns simple and anchor them to path segments; avoid quoting metacharacters that also break string literals.","Wrap the tag call in try/catch so one bad URL pattern does not abort the iteration's metric emission."],"exampleFix":"// before\npage.on('metric', (e) => e.tag({ name: 'goods', matches: [{ url: 'api/goods' }] }));\n\n// after\npage.on('metric', (e) => e.tag({ name: 'goods', matches: [{ url: '/api/goods/' }] }));","handlingStrategy":"try-catch","validationCode":"const patterns = [{ url: '/api/goods/', method: 'GET' }];\nfor (const p of patterns) {\n  try { new RegExp(p.url.replace(/^\\/(.*)\\/$/, '$1')); } catch (_) { throw new Error(`invalid url regex: ${p.url}`); }\n}\npage.on('metric', (e) => e.tag({ name: 'goods', matches: patterns }));","typeGuard":"function isRegexLiteralString(s) {\n  return typeof s === 'string' && s.length > 2 && s.startsWith('/') && s.endsWith('/') && s.lastIndexOf('/') > 0;\n}","tryCatchPattern":"page.on('metric', (e) => {\n  try { e.tag({ name: 'goods', matches }); } catch (err) { if (/matching url with regex/.test(err.message)) { console.warn(`bad regex pattern, skipping tag: ${err.message}`); return; } throw err; }\n});","preventionTips":["Write url patterns as full slash-delimited JS regex literals: '/api/goods/\\\\d+'.","Pretest each pattern with new RegExp() in the same script before registering the handler.","Keep patterns free of quotes and backslashes that also break the embedded string literal.","Wrap the tag call in try/catch so one malformed pattern does not kill the iteration."],"tags":["k6","browser","metric-event","regex","url-tagging"],"backgroundTag":"invalid-regex-pattern","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"}