Extended regular expressions with augmented syntax, named capture groups, Unicode support, and cross-browser compatibility
Improved regex execution methods with named capture support, position control, and cross-browser consistency.
Enhanced exec with named capture support and position/sticky options.
/**
* Enhanced exec with named capture support and position/sticky options
* @param str - String to search
* @param regex - Regex to search with
* @param pos - Zero-based index at which to start the search
* @param sticky - Whether the match must start at the specified position only
* @returns Match array with named capture properties on groups object, or null
*/
function exec(str: string, regex: RegExp, pos?: number, sticky?: boolean | 'sticky'): ExecArray | null;Usage Examples:
// Basic usage with named capturing group
const regex = XRegExp('U\\+(?<hex>[0-9A-F]{4})');
const match = XRegExp.exec('U+2620', regex);
console.log(match.groups.hex); // '2620'
// With position and sticky matching
let pos = 3;
const result = [];
let match;
while (match = XRegExp.exec('<1><2><3><4>5<6>', /<(\\d)>/, pos, 'sticky')) {
result.push(match[1]);
pos = match.index + match[0].length;
}
// result -> ['2', '3', '4']
// Cross-browser consistent behavior
const match2 = XRegExp.exec('test', /t(e)(s)?/);
// Properly handles nonparticipating groups across browsersEnhanced test with position/sticky support and cross-browser fixes.
/**
* Enhanced test with position/sticky support
* @param str - String to search
* @param regex - Regex to search with
* @param pos - Zero-based index at which to start the search
* @param sticky - Whether the match must start at the specified position only
* @returns Whether the regex matched the provided value
*/
function test(str: string, regex: RegExp, pos?: number, sticky?: boolean | 'sticky'): boolean;Usage Examples:
// Basic usage
XRegExp.test('abc', /c/); // true
// With position and sticky
XRegExp.test('abc', /c/, 0, 'sticky'); // false (c not at position 0)
XRegExp.test('abc', /c/, 2, 'sticky'); // true (c at position 2)
// Cross-browser consistent results
XRegExp.test('test', /e/g); // Always returns true regardless of lastIndexExecutes a provided function once per regex match.
/**
* Executes a provided function once per regex match
* @param str - String to search
* @param regex - Regex to search with
* @param callback - Function to execute for each match
*/
function forEach(str: string, regex: RegExp, callback: (match: ExecArray, index: number, str: string, regex: RegExp) => void): void;Usage Examples:
// Extract every other digit from a string
const evens = [];
XRegExp.forEach('1a2345', /\\d/, (match, i) => {
if (i % 2) evens.push(+match[0]);
});
// evens -> [2, 4]
// Process all matches with named captures
const dates = [];
XRegExp.forEach('2021-01-15 and 2022-03-20',
XRegExp('(?<year>\\\\d{4})-(?<month>\\\\d{2})-(?<day>\\\\d{2})', 'g'),
(match, i) => {
dates.push({
year: match.groups.year,
month: match.groups.month,
day: match.groups.day
});
}
);All execution methods provide enhanced named capture support:
Named captures are available on the groups property (ES2018 standard):
const regex = XRegExp('(?<name>\\\\w+)\\\\s+(?<age>\\\\d+)');
const match = XRegExp.exec('John 25', regex);
console.log(match.groups.name); // 'John'
console.log(match.groups.age); // '25'For backward compatibility, you can disable namespacing:
// Disable namespacing to use old behavior
XRegExp.uninstall('namespacing');
const match = XRegExp.exec('John 25', regex);
console.log(match.name); // 'John' (directly on match object)
console.log(match.age); // '25'Start searching from a specific position:
const text = 'abc def ghi';
const regex = /\\w+/;
const match1 = XRegExp.exec(text, regex, 0); // matches 'abc'
const match2 = XRegExp.exec(text, regex, 4); // matches 'def'
const match3 = XRegExp.exec(text, regex, 8); // matches 'ghi'Require matches to start exactly at the specified position:
const text = 'abc def';
const regex = /def/;
XRegExp.test(text, regex, 4, true); // true (def starts at position 4)
XRegExp.test(text, regex, 4, 'sticky'); // true (same as above)
XRegExp.test(text, regex, 0, 'sticky'); // false (def not at position 0)XRegExp execution methods fix several browser inconsistencies:
Properly handles undefined values for nonparticipating capturing groups:
// Native behavior varies across browsers
const nativeResult = /t(e)(s)?/.exec('te');
// Some browsers: ['te', 'e', '']
// Others: ['te', 'e', undefined]
// XRegExp provides consistent behavior
const xregexpResult = XRegExp.exec('te', /t(e)(s)?/);
// Always: ['te', 'e', undefined]Consistent lastIndex behavior across browsers:
const regex = /\\w/g;
regex.lastIndex = 5;
// XRegExp.exec preserves and properly updates lastIndex
const match = XRegExp.exec('hello world', regex);
// Consistent behavior regardless of browserInstall with Tessl CLI
npx tessl i tessl/npm-xregexp