It's only necessary to prepare the string variable first and then convert it to the RegEx.
for example:
You want to add minLength
and MaxLength
with the variable to RegEx:
function getRegEx() {
const minLength = "5"; // for exapmle: min is 5
const maxLength = "12"; // for exapmle: man is 12
var regEx = "^.{" + minLength + ","+ maxLength +"}$"; // first we make a String variable of our RegEx
regEx = new RegExp(regEx, "g"); // now we convert it to RegEx
return regEx; // In the end, we return the RegEx
}
now if you change value of MaxLength
or MinLength
, It will change in all RegExs.
Hope to be useful. Also sorry about my English.