Skip to content

Instantly share code, notes, and snippets.

@lorenzoongithub
Created June 16, 2015 20:32
Show Gist options
  • Save lorenzoongithub/9b57d1e9a5a249392b77 to your computer and use it in GitHub Desktop.
Save lorenzoongithub/9b57d1e9a5a249392b77 to your computer and use it in GitHub Desktop.
xregexp.js
//
// xregexp - The one of a kind JavaScript regular expression library
// https://github.com/slevithan/xregexp
// (this code is an adaptation on the example on the main page on github)
//
load('//cdnjs.cloudflare.com/ajax/libs/xregexp/2.0.0/xregexp-min.js');
// Using named capture and flag x (free-spacing and line comments)
var date = XRegExp('(?<year> [0-9]{4} ) -? # year \n\
(?<month> [0-9]{2} ) -? # month \n\
(?<day> [0-9]{2} ) # day ', 'x');
// XRegExp.exec gives you named backreferences on the match result
var match = XRegExp.exec('2015-02-22', date);
match.year; // -> '2015'
if (match.year != '2015') throw '';
// It also includes optional pos and sticky arguments
var pos = 3, result = [];
while (match = XRegExp.exec('<1><2><3><4>5<6>', /<(\d+)>/, pos, 'sticky')) {
result.push(match[1]);
pos = match.index + match[0].length;
} // result -> ['2', '3', '4']
if (result.length != 3) throw '';
if (result[0] != '2') throw '';
if (result[1] != '3') throw '';
if (result[2] != '4') throw '';
// XRegExp.replace allows named backreferences in replacements
x = XRegExp.replace('2015-02-22', date, '${month}/${day}/${year}'); // -> '02/22/2015'
if (x != '02/22/2015') throw '';
x = XRegExp.replace('2015-02-22', date, function(match) {
return match.month + '/' + match.day + '/' + match.year;
}); // -> '02/22/2015'
if (x != '02/22/2015') throw '';
// In fact, XRegExps compile to RegExps and work perfectly with native methods
date.test('2015-02-22'); // -> true
if (date.test('2015-02-22')==false) throw '';
// The *only* caveat is that named captures must be referenced using numbered backreferences
x = '2015-02-22'.replace(date, '$2/$3/$1'); // -> '02/22/2015'
if (x != '02/22/2015') throw ''
// If you want, you can extend native methods so you don't have to worry about this.
// Doing so also fixes numerous browser bugs in the native methods
XRegExp.install('natives');
x= '2015-02-22'.replace(date, '${month}/${day}/${year}'); // -> '02/22/2015'
if (x != '02/22/2015') throw '';
x = '2015-02-22'.replace(date, function(match) {
return match.month + '/' + match.day + '/' + match.year;
}); // -> '02/22/2015'
if (x != '02/22/2015') throw '';
date.exec('2015-02-22').year; // -> '2015'
if (date.exec('2015-02-22').year != '2015') throw '';
// Extract every other digit from a string using XRegExp.forEach
x = XRegExp.forEach('1a2345', /\d/, function(match, i) {
if (i % 2) this.push(+match[0]);
}, []); // -> [2, 4]
if (x.length != 2) throw '';
if (x[0] != 2) throw '';
if (x[1] != 4) throw '';
// Get numbers within <b> tags using XRegExp.matchChain
x = XRegExp.matchChain('1 <b>2</b> 3 <b>4 a 56</b>', [
XRegExp('(?is)<b>.*?</b>'),
/\d+/
]); // -> ['2', '4', '56']
if (x.length != 3) throw '';
if (x[0] != 2) throw '';
if (x[1] != 4) throw '';
if (x[2] != 56) throw '';
// You can also pass forward and return specific backreferences
var html = '<a href="http://xregexp.com/">XRegExp</a>' +
'<a href="http://www.google.com/">Google</a>';
x = XRegExp.matchChain(html, [
{regex: /<a href="([^"]+)">/i, backref: 1},
{regex: XRegExp('(?i)^https?://(?<domain>[^/?#]+)'), backref: 'domain'}
]); // -> ['xregexp.com', 'www.google.com']
if (x.length != 2) throw '';
if (x[0] != 'xregexp.com') throw '';
if (x[1] != 'www.google.com') throw '';
// Merge strings and regexes into a single pattern, safely rewriting backreferences
x= XRegExp.union(['a+b*c', /(dog)\1/, /(cat)\1/], 'i');
// -> /a\+b\*c|(dog)\1|(cat)\2/i
if (x != '/a\\+b\\*c|(dog)\\1|(cat)\\2/i') throw '';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment