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

More regex stuff by me:

Get the full package with addons or the latest development build at GitHub.

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 Internet Explorer 5.5+, Firefox 1.5+, Chrome, Safari 3+, and Opera 11+. You can use it with Node.js or as a RequireJS module. 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) 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('2016-02-23', date).year; // -> '2016' XRegExp.replace('2016-02-23', date, '${month}/${day}/${year}'); // -> '02/23/2016' // 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 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.

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');

Raves

DailyJS [4] XRegExp … is an impressive regular expression library that works in Node and even Internet Explorer 5.5. Andrew

Dupont [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. Smashing

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

John

Resig [1] Steve Levithan, once again, brings the JS RegExp smackdown. Brian

Dillard [2] 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. David

Herman [3] 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.