pcre-to-regexp
Converts PCRE regexp strings to JavaScript RegExp instances
Creates a JavaScript RegExp
instance from a PCRE regexp string.
Not currently feature-complete, but works enough for my needs. Send
pull requests for additional functionality!
Works with Node.js and in the browser via a CommonJS bundler like browserify.
Installation
$ npm install pcre-to-regexp
Example
A basic example of a Twitter URL parsing PCRE regexp:
var url = "%^https?:\/\/twitter\\.com(\/\\#\\!)?\/(?P<username>[a-zA-Z0-9_]{1,20})\\\/status(es)?\/(?P<id>\\d+)\/?$%ig";
var keys = [];
var re = PCRE(url, keys);
console.log(keys);
console.log(re);
var match = re.exec('https://twitter.com/tootallnate/status/481604870626349056');
console.log(match);
Use code like this if you would like to transfer the "named captures" to the
match
object:
for (var i = 0; i < keys.length; i++) {
if ('string' === typeof keys[i]) {
match[keys[i]] = match[i + 1];
}
}
console.log(match.username);
console.log(match.id);
API
PCRE(String pattern[, Array keys]) → RegExp
Returns a JavaScript RegExp instance from the given PCRE-compatible string.
Flags may be passed in after the final delimiter in the format
string.
An empty array may be passsed in as the second argument, which will be
populated with the "named capture group" names as Strings in the Array,
once the RegExp has been returned.