Command Palette

Search for a command to run...

Page Inspect

https://xregexp.com/
Internal Links
22
External Links
13
Images
2
Headings
7

Page Content

Title:JavaScript Regex :: XRegExp
Description:
HTML Size:9 KB
Markdown Size:4 KB
Fetched At:November 17, 2025

Page Structure

h1XRegExp
h1The one of a kind JavaScript regular expression library
h2What is it?
h2Features
h2Performance
h2Installation and usage
h2Named Capture Breaking Change in XRegExp 5

Markdown Content

JavaScript Regex :: XRegExp

# XRegExp
# The one of a kind JavaScript regular expression library

- Home
- API
- New syntax
- New flags
- Addons
- Usage examples ⇨
- Blog ⇨

More regex stuff by me:

• Awesome Regex
List of the best regex resources

• Regex+
JS regexes + future

“*Regular Expressions Cookbook* manages to be simultaneously accessible and almost ridiculously comprehensive.”
—Jeff Atwood

Check out Regex+, the lightweight spiritual successor to XRegExp that once again takes JavaScript regexes to the next level.

- What is it?
- Features
- Performance
- Installation and usage
- v5 breaking change

## What is it?

XRegExp provides augmented (and extensible) JavaScript regular expressions. You get modern syntax and flags beyond what browsers support natively. XRegExp is also a regex utility belt with tools to make your grepping and parsing easier, while freeing you from regex cross-browser inconsistencies and other annoyances.

XRegExp supports all native ES6 regular expression syntax. It supports ES5+ browsers (including Internet Explorer 9+), and you can use it with Node.js or as a RequireJS module. Over the years, many of XRegExp's features have been adopted by new JavaScript standards (named capturing, Unicode properties/scripts/categories, flag `s`, sticky matching, etc.), so using XRegExp can be a way to extend these features into older browsers. It's released under the MIT License.

**XRegExp lets you write regexes like this:**

// Using named capture and flag x (free-spacing and line comments)
const date = XRegExp(\`(?<year>  \[0-9\]{4} ) -?  # year
(?<month> \[0-9\]{2} ) -?  # month
(?<day>   \[0-9\]{2} )     # day\`, 'x');

**And do cool stuff like this:**

// Using named backreferences...
XRegExp.exec('2021-02-23', date).groups.year;
// -> '2021'
XRegExp.replace('2021-02-23', date, '$<month>/$<day>/$<year>');
// -> '02/23/2021'

// Finding matches within matches, while passing forward and returning specific backreferences
const html = '<a href="http://xregexp.com/api/">XRegExp</a>' +
'<a href="http://www.google.com/">Google</a>';
XRegExp.matchChain(html, \[
{regex: /<a href="(\[^"\]+)">/i, backref: 1},
{regex: XRegExp('(?i)^https?://(?<domain>\[^/?#\]+)'), backref: 'domain'}
\]);
// -> \['xregexp.com', 'www.google.com'\]

**Check out more usage examples on GitHub ⇨.**

## Features

- Adds **new regex and replacement text syntax**, including comprehensive support for **named capture**.
- Adds **new regex flags**: `s`, to make **dot match all** characters; `x`, for **free-spacing** and line comments; `n`, for **explicit capture** mode; and `A`, for **astral** mode (full 21-bit Unicode matching).
- Provides a **suite of functions** that make complex regex processing easier.
- Supports **addons** that add even more new regex syntax, flags, and methods. Offical addons support **Unicode**, **recursive matching**, and **grammatical patterns**.

## Performance

XRegExp compiles to native `RegExp` objects. Therefore regexes built with XRegExp perform just as fast as native regular expressions. There is a tiny extra cost when compiling a pattern for the first time.

## Installation and usage

In browsers (bundle XRegExp with all of its addons):

<script src="https://unpkg.com/xregexp/xregexp-all.js"></script>

Using npm:

npm install xregexp

In Node.js:

const XRegExp = require('xregexp');

## Named Capture Breaking Change in XRegExp 5

XRegExp 5 introduced a breaking change where named backreference properties now appear on the result's `groups` object (following ES2018), rather than directly on the result. To restore the old handling so you don't need to update old code, run the following line after importing XRegExp:

XRegExp.uninstall('namespacing');

XRegExp 4.1.0 and later allow introducing the new behavior without upgrading to XRegExp 5 by running `XRegExp.install('namespacing')`.

Following is the most commonly needed change to update code for the new behavior:

// Change this
const name = XRegExp.exec(str, regexWithNamedCapture).name;

// To this
const name = XRegExp.exec(str, regexWithNamedCapture).groups.name;

See the README on GitHub ⇨ for more examples of using named capture with `XRegExp.exec` and `XRegExp.replace`.

The world's greatest regex tester includes XRegExp as a supported flavor. Get RegexBuddy.

© Steven Levithan :: GitHub