From 17028a3f86f97b9d2ea3a0d31346ed62885f65c3 Mon Sep 17 00:00:00 2001 From: dodococo Date: Wed, 7 Oct 2020 12:03:53 +0530 Subject: [PATCH] Input Widget: Parse regexp before regex validation (#884) Fixes #697 --- app/client/src/widgets/InputWidget.tsx | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/app/client/src/widgets/InputWidget.tsx b/app/client/src/widgets/InputWidget.tsx index c260a1fc7d..2c8a35ce23 100644 --- a/app/client/src/widgets/InputWidget.tsx +++ b/app/client/src/widgets/InputWidget.tsx @@ -51,6 +51,21 @@ class InputWidget extends BaseWidget { return { isValid: `{{ function(){ + let parsedRegex = null; + if (this.regex) { + // break up the regexp pattern into 4 parts: given regex, regex prefix , regex pattern, regex flags + // Example /appsmith/i will be split into ["/appsmith/gi", "/", "appsmith", "gi"] + const regexParts = this.regex.match(/(\\/?)(.+)\\1([a-z]*)/i); + if (regexParts === null) { + parsedRegex = new RegExp(this.regex); + } + // if we don't have a regex flags (gmisuy), convert provided string into regexp directly + if (regexParts[3] && !/^(?!.*?(.).*?\\1)[gmisuy]+$/.test(regexParts[3])) { + parsedRegex = RegExp(this.regex); + } + // if we have a regex flags, use it to form regexp + parsedRegex = new RegExp(regexParts[2], regexParts[3]); + } if (this.inputType === "EMAIL") { const emailRegex = new RegExp(/^\\w+([\\.-]?\\w+)*@\\w+([\\.-]?\\w+)*(\\.\\w{2,3})+$/); return emailRegex.test(this.text); @@ -60,16 +75,16 @@ class InputWidget extends BaseWidget { } else if (this.isRequired) { if(this.text && this.text.length) { - if(this.regex) { - return new RegExp(this.regex).test(this.text) + if (parsedRegex) { + return parsedRegex.test(this.text) } else { return true; } } else { return false; } - } if (this.regex) { - return new RegExp(this.regex).test(this.text) + } if (parsedRegex) { + return parsedRegex.test(this.text) } else { return true; }