Extended regular expressions with augmented syntax, named capture groups, Unicode support, and cross-browser compatibility
Core functionality for creating extended regular expressions with additional syntax and flags beyond native JavaScript support.
Creates an extended regular expression object with additional syntax and flags.
/**
* Creates an extended regular expression object with additional syntax and flags
* @param pattern - Regex pattern string or existing regex object to copy
* @param flags - Any combination of flags
* @returns Extended regular expression object
*/
function XRegExp(pattern: string | RegExp, flags?: string): RegExp;Supported Flags:
d (indices), g (global), i (ignore case), m (multiline), u (unicode), y (sticky)n (named capture only), s (dot matches all), x (free-spacing), A (astral Unicode)Usage Examples:
// Basic usage with named capture
const regex = XRegExp('(?<word>\\w+)', 'g');
// Free-spacing mode with comments
const date = XRegExp(`
(?<year> [0-9]{4} ) -? # year
(?<month> [0-9]{2} ) -? # month
(?<day> [0-9]{2} ) # day
`, 'x');
// Copy existing regex
const copy = XRegExp(/test/gi);
// Dot matches all (including newlines)
const multiline = XRegExp('start.*end', 's');
// Unicode property matching
const unicode = XRegExp('\\p{Letter}+', 'A');Caches and returns cached XRegExp objects for performance.
/**
* Caches and returns the result of calling XRegExp(pattern, flags)
* @param pattern - Regex pattern string
* @param flags - Any combination of XRegExp flags
* @returns Cached XRegExp object
*/
function cache(pattern: string, flags?: string): RegExp;Usage Examples:
// Compiled once, reused multiple times
let match;
while (match = XRegExp.cache('.', 'gs').exec('abc')) {
// The regex is compiled once only
}Helper functions for regex manipulation and inspection.
/**
* Copies a regex object and adds global flag
* @param regex - Regex to globalize
* @returns Copy of regex with global flag added
*/
function globalize(regex: RegExp): RegExp;
/**
* Cross-frame regex detection
* @param value - Object to check
* @returns Whether the object is a RegExp
*/
function isRegExp(value: any): value is RegExp;
/**
* XRegExp version number
*/
const version: string;Usage Examples:
// Add global flag to existing regex
const globalRegex = XRegExp.globalize(/test/i);
console.log(globalRegex.global); // true
// Safe regex detection across frames
XRegExp.isRegExp(/test/); // true
XRegExp.isRegExp('string'); // false
// Get version
console.log(XRegExp.version); // '5.1.2'XRegExp provides enhanced named capture group support:
Named Capture Syntax:
(?<name>pattern) - Create named capture group\\k<name> - Named backreference$<name> or ${name} - Named replacementUsage Examples:
// Named capture groups
const nameRegex = XRegExp('(?<first>\\w+) (?<last>\\w+)');
const match = XRegExp.exec('John Smith', nameRegex);
console.log(match.groups.first); // 'John'
console.log(match.groups.last); // 'Smith'
// Named backreferences
const doubled = XRegExp('(?<word>\\w+)\\s+\\k<word>', 'i');
doubled.test('hello hello'); // true
// Named replacements
XRegExp.replace('John Smith', nameRegex, '$<last>, $<first>');
// Result: 'Smith, John'Allows whitespace and comments in regex patterns:
const complexRegex = XRegExp(`
^ # Start of string
(?<protocol> https?) # Protocol (http or https)
:// # Separator
(?<domain> # Domain group
[\\w.-]+ # Domain characters
)
(?<path> /.*)? # Optional path
$ # End of string
`, 'x');Makes dot (.) match newline characters:
const multilineRegex = XRegExp('start.*end', 's');
multilineRegex.test('start\\nend'); // trueConverts all capturing groups to non-capturing except named ones:
const namedOnly = XRegExp('(\\d+)(?<name>\\w+)(\\d+)', 'n');
// Equivalent to: (?:\\d+)(?<name>\\w+)(?:\\d+)Install with Tessl CLI
npx tessl i tessl/npm-xregexp