Skip to content

fix(regex): honour leading inline flags like (?i) on .NET Regex#3305

Merged
dgozman merged 1 commit intomicrosoft:mainfrom
dgozman:fix-3236
May 1, 2026
Merged

fix(regex): honour leading inline flags like (?i) on .NET Regex#3305
dgozman merged 1 commit intomicrosoft:mainfrom
dgozman:fix-3236

Conversation

@dgozman
Copy link
Copy Markdown
Collaborator

@dgozman dgozman commented May 1, 2026

Summary

  • Regex.ToString() returns the pattern verbatim (including any leading inline modifier like (?i)), but Regex.Options only carries the constructor flags. So a pattern like new Regex(\"(?i).+\\.css$\") was sent to the JS-side matcher with no i flag and a (?i) group that JS regex syntax rejects.
  • New Regex.GetSourceAndFlags() helper strips a leading (?ism-ism) modifier group and merges the enabled/disabled bits into the wire flags. All 7 sites that previously sent (regex.ToString(), regex.Options.GetInlineFlags()) now use it (route handlers, web-socket route handler, AssertionsBase, Locator selector escaping, BrowserContext.ClearCookiesAsync/HarStart, EvaluateArgumentValueConverter).
  • Unsupported inline flags (n, x) throw ArgumentException, matching the existing behavior for unsupported RegexOptions.

Fixes #3236

Strips a leading inline flag group `(?ism-ism)` from the regex source
before sending to the driver, and merges those flags with `Regex.Options`.
The pattern is otherwise sent verbatim and JS-side regex syntax does not
support `(?i)` modifier groups, so they previously failed with
"Invalid group".

Fixes microsoft#3236
@dgozman dgozman merged commit baf5e4c into microsoft:main May 1, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Does not honour valid PCRE modifiers

2 participants