Fork me on GitHub

Get releases or the latest development build at GitHub.

Check out regex, the lightweight and modern spiritual successor to XRegExp that once again takes JavaScript regexes to the next level.

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

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.