# `PlaywrightEx.RegExp`
[🔗](https://github.com/ftes/playwright_ex/blob/v0.17.0-rc.5/lib/playwright_ex/reg_exp.ex#L1)

A JS `RegExp`, for patterns Elixir's `Regex` cannot express:

    Page.get_by_text(page, PlaywrightEx.RegExp.new("a[^]b", "v"))

Wherever Playwright takes a `RegExp` (selectors and `get_by_*` options, `has_text`, `expect`
matchers, URL matchers, `clear_cookies`, HAR `url:`, `evaluate` arguments), it takes either:

  * a `Regex` (`~r/.../`), sent as a JS regex with its modifiers `i`, `m`, `s`, `u`. Options
    and PCRE-only syntax JS cannot run (`x`, `U`, `\A`, `\z`, atomic groups, possessive
    quantifiers, inline `(?i)`, recursion, `\K`) raise `ArgumentError`;
  * a `PlaywrightEx.RegExp` (`new/2`), sent as is: JS-only syntax such as `[^]`, the `v`, `y`,
    `d` and `g` flags, or variable-length lookbehind (`(?<=a+)`) works.

Strings are always literal text (or globs, for URLs), never patterns.

The source is not validated here: the browser or driver reports JS syntax errors. Matching in
Elixir (`wait_for_url`, `Page.frame(url:)`, routes, `wait_for_request`) compiles a `RegExp` to
a `Regex`: `[^]`, `\uXXXX` and identity escapes are translated, the `y` flag anchors, `g` and
`d` do not matter; a pattern PCRE cannot run (the `v` flag, unbounded lookbehind) raises
`ArgumentError`. JS regexes returned by the driver (`evaluate`) are always a
`PlaywrightEx.RegExp` (lossless); `to_regex/1` or `compile!/1` gives a `Regex` when PCRE can run
it.

# `t`

```elixir
@type t() :: %PlaywrightEx.RegExp{flags: String.t(), source: String.t()}
```

A JS regex: `source` as JS `re.source` (`/` escaped), `flags` in JS order.

# `compile!`

```elixir
@spec compile!(t()) :: Regex.t()
```

The `Regex` that matches as `re` does in JS. Raises `ArgumentError` if PCRE cannot run it
(the `v` flag, unbounded lookbehind, ...).

# `flags`

```elixir
@spec flags(t()) :: String.t()
```

`re.flags`.

# `new`

```elixir
@spec new(String.t(), String.t()) :: t()
```

`new RegExp(source, flags)`: raises `ArgumentError` on unknown or repeated flags (JS:
`dgimsuvy`), and on `u` with `v`. `/` is escaped in the source, an empty one is `(?:)`.

    iex> PlaywrightEx.RegExp.new("a/b", "iv")
    %PlaywrightEx.RegExp{source: "a\\/b", flags: "iv"}

# `source`

```elixir
@spec source(t()) :: String.t()
```

`re.source`.

# `to_regex`

```elixir
@spec to_regex(t()) :: {:ok, Regex.t()} | {:error, String.t()}
```

The `Regex` that matches as `re` does in JS (source translated, e.g. `[^]` to `[\s\S]`;
compiled with `:unicode`, `:dollar_endonly`), or `{:error, reason}` if PCRE cannot run it.

# `to_string`

```elixir
@spec to_string(t()) :: String.t()
```

`String(re)`: `/source/flags`.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
