', start: 8, end: 13},
{name: 'match', value: '
an
', start: 13, end: 27},
{name: 'right', value: '
', start: 27, end: 33},
{name: 'between', value: ' example', start: 33, end: 41}
] */
// Omitting unneeded parts with null valueNames, and using escapeChar
str = '...{1}\\{{function(x,y){return y+x;}}';
XRegExp.matchRecursive(str, '{', '}', 'g', {
valueNames: ['literal', null, 'value', null],
escapeChar: '\\'
});
/* -> [
{name: 'literal', value: '...', start: 0, end: 3},
{name: 'value', value: '1', start: 4, end: 5},
{name: 'literal', value: '\\{', start: 6, end: 8},
{name: 'value', value: 'function(x,y){return y+x;}', start: 9, end: 35}
] */
// Sticky mode via flag y
str = '<1><<<2>>><3>4<5>';
XRegExp.matchRecursive(str, '<', '>', 'gy');
// -> ['1', '<<2>>', '3']
~~~
`XRegExp.matchRecursive` throws an error if it sees an unbalanced delimiter in the target string.
### XRegExp Prototype Methods
In browsers, first include the script:
~~~ html
~~~
New XRegExp regexes then gain a collection of useful methods: `apply`, `call`, `forEach`, `globalize`, `xexec`, and `xtest`.
~~~ js
// To demonstrate the call method, let's first create the function we'll be using...
function filter(array, fn) {
var res = [];
array.forEach(function (el) {if (fn.call(null, el)) res.push(el);});
return res;
}
// Now we can filter arrays using functions and regexes
filter(['a', 'ba', 'ab', 'b'], XRegExp('^a')); // -> ['a', 'ab']
~~~
Native `RegExp` objects copied by `XRegExp` are augmented with any `XRegExp.prototype` methods. The following lines therefore work equivalently:
~~~ js
XRegExp('[a-z]', 'ig').xexec('abc');
XRegExp(/[a-z]/ig).xexec('abc');
XRegExp.globalize(/[a-z]/i).xexec('abc');
~~~
## Installation and usage
In browsers:
~~~ html
~~~
Or, to bundle XRegExp with all of its addons:
~~~ html
~~~
Using [npm](http://npmjs.org/):
~~~ bash
npm install xregexp
~~~
In [Node.js](http://nodejs.org/) and [CommonJS module](http://wiki.commonjs.org/wiki/Modules) loaders:
~~~ js
var XRegExp = require('xregexp').XRegExp;
~~~
### Running tests on the server with npm
~~~ bash
npm install -g qunit # needed to run the tests
npm test # in the xregexp root
~~~
If XRegExp was not installed using npm, just open `tests/index.html` in your browser.
## &c
**Lookbehind:** A [collection of short functions](https://gist.github.com/2387872) is available that makes it easy to simulate infinite-length leading lookbehind.
## Changelog
* Releases: [Version history](http://xregexp.com/history/).
* Upcoming: [Milestones](https://github.com/slevithan/XRegExp/issues/milestones), [Roadmap](https://github.com/slevithan/XRegExp/wiki/Roadmap).
## About
XRegExp and addons copyright 2007-2012 by [Steven Levithan](http://stevenlevithan.com/).
Tools: Unicode range generators by [Mathias Bynens](http://mathiasbynens.be/). Source file concatenator by [Bjarke Walling](http://twitter.com/walling).
Prior art: `XRegExp.build` inspired by [Lea Verou](http://lea.verou.me/)'s [RegExp.create](http://lea.verou.me/2011/03/create-complex-regexps-more-easily/). `XRegExp.union` inspired by [Ruby](http://www.ruby-lang.org/). XRegExp's syntax extensions come from Perl, .NET, etc.
All code released under the [MIT License](http://mit-license.org/).
Fork me to show support, fix, and extend.