regex

Members

(static, constant) RE_ATTRIBUTES

Regular expression for matching a HTML attribute and tag names, also for matching shortcode attributes and names

Source:

(static, constant) RE_ATTRIBUTE_WITHOUT_VALUE

Regular expression for matching a single attribute without value

Source:

(static, constant) RE_ATTRIBUTE_WITH_VALUE

Regular expression for matching a single attribute with value

Source:

(static, constant) RE_FIRST_OR_LAST_QUOTE

Regular expression for matching the first or last quote of a string used for removing them

Source:

(static, constant) RE_IMAGE

Regular expression for matching a image URLs

Source:

(static, constant) RE_URL_PARAMETER

Regular expression for matching a URL parameters

Source:

(static, constant) RE_VIDEO

Regular expression for matching a video URLs, tolerates a trailing query string or hash

Source:

(static, constant) RE_VIMEO

Regular expression for matching a Vimeo video links and extracting their ID, works with both embed and watch URLs, channels and groups

Source:

(static, constant) RE_YOUTUBE

Regular expression for matching a YouTube video links and extracting their ID, works with both embed and watch URLs

Source:

Methods

(static) convertGlobToRegex(glob, anchoredopt) → {RegExp|Object|null}

Convert a glob pattern to a regular expression but unanchored (doesn't add ^ or $) by default

Supports standard glob syntax (*, **, ?, [...], {...}), extended glob patterns (?(pattern), *(pattern), +(pattern), @(pattern), !(pattern)), and backslash escaping.

Alternative packages to consider: micromatch, picomatch, minimatch, glob-to-regexp...

Source:
Parameters:
Name Type Attributes Default Description
glob string

The glob pattern to convert

anchored boolean <optional>
false

Whether to anchor the regex with ^ and $ for exact matching

Returns:
Type:
RegExp | Object | null

A RegExp (or object with test method for negation patterns), or null if the pattern is invalid

Example
convertGlobToRegex('*.js').test('app.js') // true
convertGlobToRegex('src/**\/*.ts').test('src/lib/utils.ts') // true
convertGlobToRegex('*.{js,ts}').test('app.ts') // true
convertGlobToRegex('!(test).js').test('app.js') // true
convertGlobToRegex('!(test).js').test('test.js') // false

(static) escapeRegExp(string) → {string}

Escape special characters in a string to be used in a regular expression

Source:
Parameters:
Name Type Description
string string

The string to escape

Returns:
Type:
string

The escaped string

Example
escapeRegExp('hello world') // 'hello world'
escapeRegExp('hello.*+?^${}()|[]\\world') // 'hello\\.\\*\\+\\?\\^\\$\\{\\}\\(\\)\\|\\[\\]\\\\world'