Playwright.RegExp (Playwright v1.65.0-rc.3)

Copy Markdown View Source

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

Page.get_by_text(page, Playwright.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 Playwright.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.

new/1 does that conversion for a Regex of your own, raising as Playwright would, and compile!/1 then matches it in Elixir as the browser does.

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: [^], \s/\S (JS's Unicode whitespace), \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. On OTP 27, whose PCRE has Latin-1 character tables, \w and \b also take Latin-1 letters (é); from OTP 28 they are ASCII, as in JS. JS regexes returned by the driver (evaluate) are always a Playwright.RegExp (lossless); to_regex/1 or compile!/1 gives a Regex when PCRE can run it.

Summary

Types

t()

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

Functions

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

re.flags.

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

re.source.

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.

String(re): /source/flags.

Types

t()

@type t() :: %Playwright.RegExp{flags: String.t(), source: String.t()}

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

Functions

compile!(re)

@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(reg_exp)

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

re.flags.

new(pattern, flags \\ nil)

@spec new(String.t() | Regex.t() | t(), String.t() | nil) :: t()

new RegExp(pattern, 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> Playwright.RegExp.new("a/b", "iv")
#Playwright.RegExp</a\/b/iv>

pattern may be a regex, as in JS (new RegExp(re)): its source, with its flags unless flags is given. A Regex is converted as wherever Playwright takes one: its options i, m, s, u become flags, and other options or PCRE-only syntax raise ArgumentError (see the moduledoc). Under u, escaped punctuation JS's u mode rejects loses its redundant backslash, so Regex.escape/1's output works. With compile!/1, Elixir matches it as the browser does.

iex> Playwright.RegExp.new(~r/^#{Regex.escape("Owl #1 - Hedwig")}$/u)
#Playwright.RegExp</^Owl #1 - Hedwig$/u>

iex> Playwright.RegExp.new(~r"a/b"i)
#Playwright.RegExp</a\/b/i>

source(reg_exp)

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

re.source.

to_regex(reg_exp)

@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(reg_exp)

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

String(re): /source/flags.