Fork me on GitHub

Get XRegExp 2.0: minified (3.5 KB gzipped), or with comments. Get the full package or the latest development build at GitHub.

What is it?

XRegExp is an open source (MIT License) JavaScript library that provides augmented and extensible regular expressions. You get new syntax, flags, and methods beyond what browsers support natively. XRegExp is also a regex utility belt with tools to make your client-side grepping simpler and more powerful, while freeing you from worrying about pesky cross-browser inconsistencies and the dubious lastIndex property.

XRegExp supports all native ES5 regular expression syntax. It works with Internet Explorer 5.5+, Firefox 1.5+, Chrome, Safari 3+, and Opera 11+. You can also use it on the server with Node.js.

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'

// Matchception: Finding matches within matches, while passing forward and
// returning specific backreferences
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 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. If you want, you can use XRegExp.cache to avoid incurring the compilation cost again for a given pattern and flags, across your entire application.

Node.js and npm

To install XRegExp and its addons on the server using npm, run npm install xregexp. You can then load it using Node.js and other CommonJS module loaders with this:

var XRegExp = require('xregexp').XRegExp;

Raves

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.

XRegExp is used by the BBC, IBM, Kayako, RegexPal, SyntaxHighlighter, RegexPlanet, and JRX, among others.

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