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; }