diff --git a/public/app/core/specs/kbn.test.ts b/public/app/core/specs/kbn.test.ts new file mode 100644 index 00000000000..c97e2e1101a --- /dev/null +++ b/public/app/core/specs/kbn.test.ts @@ -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(); + }); +}); diff --git a/public/app/core/utils/kbn.ts b/public/app/core/utils/kbn.ts index 887c30229d3..43886fafd07 100644 --- a/public/app/core/utils/kbn.ts +++ b/public/app/core/utils/kbn.ts @@ -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]); };