PlaywrightEx.RegExp (PlaywrightEx v0.17.0-rc.5)

Copy Markdown View Source

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.

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(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 (?:).

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() :: %PlaywrightEx.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(source, flags \\ "")

@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(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.