Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tootallnate/pcre-to-regexp
Converts PCRE regexp strings to JavaScript RegExp instances
https://github.com/tootallnate/pcre-to-regexp
Last synced: 4 days ago
JSON representation
Converts PCRE regexp strings to JavaScript RegExp instances
- Host: GitHub
- URL: https://github.com/tootallnate/pcre-to-regexp
- Owner: TooTallNate
- License: mit
- Created: 2014-07-09T05:45:01.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2023-01-07T09:00:12.000Z (almost 2 years ago)
- Last Synced: 2024-12-10T14:05:53.558Z (16 days ago)
- Language: JavaScript
- Size: 317 KB
- Stars: 22
- Watchers: 3
- Forks: 6
- Open Issues: 12
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
pcre-to-regexp
==============
### Converts PCRE regexp strings to JavaScript RegExp instances
[![Build Status](https://github.com/TooTallNate/pcre-to-regexp/workflows/Node%20CI/badge.svg)](https://github.com/TooTallNate/pcre-to-regexp/actions?workflow=Node+CI)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
------------``` bash
$ npm install pcre-to-regexp
```Example
-------A basic example of a Twitter URL parsing PCRE regexp:
``` js
var url = "%^https?:\/\/twitter\\.com(\/\\#\\!)?\/(?P[a-zA-Z0-9_]{1,20})\\\/status(es)?\/(?P\\d+)\/?$%ig";// parse the PCRE regexp into a JS RegExp
var keys = [];
var re = PCRE(url, keys);console.log(keys);
// [ , 'username', , 'id' ]console.log(re);
// /^https?://twitter\.com(/\#\!)?/([a-zA-Z0-9_]{1,20})\/status(es)?/(\d+)/?$/givar match = re.exec('https://twitter.com/tootallnate/status/481604870626349056');
console.log(match);
// [ 'https://twitter.com/tootallnate/status/481604870626349056',
// undefined,
// 'tootallnate',
// undefined,
// '481604870626349056',
// index: 0,
// input: 'https://twitter.com/tootallnate/status/481604870626349056' ]
```Use code like this if you would like to transfer the "named captures" to the
`match` object:``` js
// example of copying the named captures as keys on 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);
// 'tootallnate'console.log(match.id);
// '481604870626349056'
```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.