JAVASCRIPT   56

regex.js

Guest on 8th October 2021 06:24:11 PM

  1. var safe = require('../');
  2. var test = require('tape');
  3.  
  4. var good = [
  5.     /\bOakland\b/,
  6.     /\b(Oakland|San Francisco)\b/i,
  7.     /^\d+1337\d+$/i,
  8.     /^\d+(1337|404)\d+$/i,
  9.     /^\d+(1337|404)*\d+$/i,
  10.     RegExp(Array(26).join('a?') + Array(26).join('a')),
  11. ];
  12.  
  13. test('safe regex', function (t) {
  14.     t.plan(good.length);
  15.     good.forEach(function (re) {
  16.         t.equal(safe(re), true);
  17.     });
  18. });
  19.  
  20.  
  21. var bad = [
  22.     /^(a?){25}(a){25}$/,
  23.     RegExp(Array(27).join('a?') + Array(27).join('a')),
  24.     /(x+x+)+y/,
  25.     /foo|(x+x+)+y/,
  26.     /(a+){10}y/,
  27.     /(a+){2}y/,
  28.     /(.*){1,32000}[bc]/
  29. ];
  30.  
  31. test('unsafe regex', function (t) {
  32.     t.plan(bad.length);
  33.     bad.forEach(function (re) {
  34.         t.equal(safe(re), false);
  35.     });
  36. });
  37.  
  38. var invalid = [
  39.     '*Oakland*',
  40.     'hey(yoo))',
  41.     'abcde(?>hellow)',
  42.     '[abc'
  43. ];
  44.  
  45. test('invalid regex', function (t) {
  46.     t.plan(invalid.length);
  47.     invalid.forEach(function (re) {
  48.         t.equal(safe(re), false);
  49.     });
  50. });

Raw Paste


Login or Register to edit or fork this paste. It's free.