# Regular-expression syntax inside a shExpMatch pattern

Source: https://findproxyforurl.net/rules/regex-syntax-in-shexpmatch/ · last updated 2026-10-11

Rule PAC-K015 (`regex-syntax-in-shexpmatch`), severity medium, category: Compatibility across engines.

shExpMatch(host, "(www|mail).corp.example") works in Chromium, Firefox and pacparser only because their helper turns the glob into a JavaScript RegExp without escaping ( ) and |. That is an accident, not the PAC contract; Windows (WinHTTP) treats the characters literally and never matches.

## Why it matters

The PAC specification defines `shExpMatch` as shell-style matching with `*` and `?`. The
JavaScript helper library shared by Chromium, Firefox and pacparser escapes dots, rewrites
`*` and `?` and then builds a `RegExp`, so alternation `(a|b)` and other regex syntax leak
through and work. Windows (WinHTTP) does not: in the lab on Windows 11 (2026-10-11) its
`shExpMatch` knew only `*` and `?`, took `[ ] + ( ) | { } $` as ordinary characters, and
gave `^` a special meaning of its own. `shExpMatch("ab", "a|b")` and
`shExpMatch("abc", "[ab]bc")` were false there, while `shExpMatch("a+c", "a+c")` was true. A
PAC relying on alternation therefore has an engine dependency that is invisible in browser
tests. Writing the alternatives as separate `shExpMatch` or `host ==` clauses is portable and
no slower.


## How to fix

Spell the alternatives out as separate conditions joined with ||.

## Bad

```js
function FindProxyForURL(url, host) {
  if (shExpMatch(host, "(www|mail).corp.example")) {
    return "DIRECT";
  }
  return "PROXY proxy.corp.example:8080";
}
```

## Good

```js
function FindProxyForURL(url, host) {
  if (host == "www.corp.example" || host == "mail.corp.example") {
    return "DIRECT";
  }
  return "PROXY proxy.corp.example:8080";
}
```

## Engine behaviour

- chromium: shExpMatch converts the glob to a RegExp without escaping ( ) | ; regex alternation works. (source: code, verified 2026-10-04)
- firefox: Same helper-library approach (ascii_pac_utils.js); regex alternation works. (source: code, unverified)
- winhttp: Only * and ? are wildcards; [ ] + ( ) | { } $ are matched literally, so regex alternation never matches. A ^ in the pattern changes the match (shExpMatch("x^", "x^") is false). Matching is case-sensitive. (source: lab, verified 2026-10-11)
