Functional Regex
Functional Regex simplifies the way you work with global regular expressions in JavaScript.
Functional Regex aims to simplify the process of iterating over a global regular expression. It is
often easier to treat the results of a globally matched regular expression as an array. It is then
possible to use map
, reduce
, forEach
, some
, filter
, etc on the results.
Example
When looking for something like /foo(test)/g it's necessary to do:
var regex = /foo/g result; while result = regex !== null // do something with result;
Wouldn't it be nice if we could do something more like:
var fregex = ; ; // == ['1', '2']
Because it's simply an array, we can use forEach
and map
on it as well as other array methods.
var fregex = ; ; // == [1, 2]
Installation
npm install --save functional-regex
Usage
There are three ways to use Functional Regex.
- Standalone (default, because extending native prototypes is evil)
- Legacy (also does not modify prototypes)
- Augmenting the RegExp prototype
1. Standalone
var fregex = ; ; // => array
2. Standalone (legacy)
var fregex = ; fregex;fregex;
3. RegExp prototype
; var regex = /foo/g; regex;regex;
Contributing
Open an issue, or submit a pull-request.