Merge branch 'SamuelToh-11780_wrong_repexp_in_series'

This commit is contained in:
Torkel Ödegaard 2019-03-08 13:55:53 +01:00
commit 747aa82272
2 changed files with 20 additions and 0 deletions

View File

@ -0,0 +1,15 @@
import kbn from '../utils/kbn';
describe('stringToJsRegex', () => {
it('should parse the valid regex value', () => {
const output = kbn.stringToJsRegex('/validRegexp/');
expect(output).toBeInstanceOf(RegExp);
});
it('should throw error on invalid regex value', () => {
const input = '/etc/hostname';
expect(() => {
kbn.stringToJsRegex(input);
}).toThrow();
});
});

View File

@ -234,6 +234,11 @@ kbn.stringToJsRegex = str => {
}
const match = str.match(new RegExp('^/(.*?)/(g?i?m?y?)$'));
if (!match) {
throw new Error(`'${str}' is not a valid regular expression.`);
}
return new RegExp(match[1], match[2]);
};