Fork me on GitHub

2015-09-24: Get XRegExp 3.0.0: minified (4.25 KB gzipped) or with comments. Get the full package or the latest development build at GitHub.

What is it?

XRegExp provides augmented (and extensible) JavaScript regular expressions. You get new modern syntax and flags beyond what browsers support natively. XRegExp is also a regex utility belt with tools to make your client-side grepping and parsing easier, while freeing you from worrying about pesky aspects of JavaScript regexes like cross-browser inconsistencies and manually manipulating lastIndex.

XRegExp supports all native ES6 regular expression syntax. It supports Internet Explorer 5.5+, Firefox 1.5+, Chrome, Safari 3+, and Opera 11+. You can also use it with Node.js, or as a RequireJS module. It's released under the terms of the MIT license.

XRegExp lets you write regexes like this:

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

And do cool stuff like this:

// Using named backreferences...
XRegExp.exec('2012-06-10', date).year;
// -> '2012'
XRegExp.replace('2012-06-10', date, '${month}/${day}/${year}');
// -> '06/10/2012'

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

Check out more usage examples on GitHub ⇨.



XRegExp regexes compile to native RegExp objects, and therefore perform just as fast as native regular expressions. There is a tiny extra cost when compiling a pattern for the first time.

Node.js and npm

To install XRegExp and its addons using npm, run npm install xregexp. You can then load it using:

var XRegExp = require('xregexp');

// Alternatively, you can use the CommonJS style which also works
var XRegExp = require('xregexp').XRegExp;


XRegExp … is an impressive regular expression library that works in Node and even Internet Explorer 5.5.

XRegExp … does some killer stuff that the native JS RegExp does not … it's a must-have for grepping and/or parsing on the client side.

XRegExp adds more power to RegExp with features that will be part of the browsers of tomorrow.

Steve Levithan, once again, brings the JS RegExp smackdown.

For those of you so advanced in your RegEx powers that you've hit the limitations of the built-in JavaScript implementation, check out XRegExp, an open-source regular-expression library that supports named capture and other advanced features.

XRegExp [is] amazing work. ES6's new back-tick literals should add some convenience, too.

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