[6a3a178] | 1 | # safe-regex
|
---|
| 2 |
|
---|
| 3 | detect potentially
|
---|
| 4 | [catastrophic](http://regular-expressions.mobi/catastrophic.html)
|
---|
| 5 | [exponential-time](http://perlgeek.de/blog-en/perl-tips/in-search-of-an-exponetial-regexp.html)
|
---|
| 6 | regular expressions by limiting the
|
---|
| 7 | [star height](https://en.wikipedia.org/wiki/Star_height) to 1
|
---|
| 8 |
|
---|
| 9 | WARNING: This module merely *seems* to work given all the catastrophic regular
|
---|
| 10 | expressions I could find scouring the internet, but I don't have enough of a
|
---|
| 11 | background in automata to be absolutely sure that this module will catch all
|
---|
| 12 | exponential-time cases.
|
---|
| 13 |
|
---|
| 14 | [![browser support](https://ci.testling.com/substack/safe-regex.png)](https://ci.testling.com/substack/safe-regex)
|
---|
| 15 |
|
---|
| 16 | [![build status](https://secure.travis-ci.org/substack/safe-regex.png)](http://travis-ci.org/substack/safe-regex)
|
---|
| 17 |
|
---|
| 18 | # example
|
---|
| 19 |
|
---|
| 20 | ``` js
|
---|
| 21 | var safe = require('safe-regex');
|
---|
| 22 | var regex = process.argv.slice(2).join(' ');
|
---|
| 23 | console.log(safe(regex));
|
---|
| 24 | ```
|
---|
| 25 |
|
---|
| 26 | ```
|
---|
| 27 | $ node safe.js '(x+x+)+y'
|
---|
| 28 | false
|
---|
| 29 | $ node safe.js '(beep|boop)*'
|
---|
| 30 | true
|
---|
| 31 | $ node safe.js '(a+){10}'
|
---|
| 32 | false
|
---|
| 33 | $ node safe.js '\blocation\s*:[^:\n]+\b(Oakland|San Francisco)\b'
|
---|
| 34 | true
|
---|
| 35 | ```
|
---|
| 36 |
|
---|
| 37 | # methods
|
---|
| 38 |
|
---|
| 39 | ``` js
|
---|
| 40 | var safe = require('safe-regex')
|
---|
| 41 | ```
|
---|
| 42 |
|
---|
| 43 | ## var ok = safe(re, opts={})
|
---|
| 44 |
|
---|
| 45 | Return a boolean `ok` whether or not the regex `re` is safe and not possibly
|
---|
| 46 | catastrophic.
|
---|
| 47 |
|
---|
| 48 | `re` can be a `RegExp` object or just a string.
|
---|
| 49 |
|
---|
| 50 | If the `re` is a string and is an invalid regex, returns `false`.
|
---|
| 51 |
|
---|
| 52 | * `opts.limit` - maximum number of allowed repetitions in the entire regex.
|
---|
| 53 | Default: `25`.
|
---|
| 54 |
|
---|
| 55 | # install
|
---|
| 56 |
|
---|
| 57 | With [npm](https://npmjs.org) do:
|
---|
| 58 |
|
---|
| 59 | ```
|
---|
| 60 | npm install safe-regex
|
---|
| 61 | ```
|
---|
| 62 |
|
---|
| 63 | # license
|
---|
| 64 |
|
---|
| 65 | MIT
|
---|