0

I'm working on a validation in react app That checks if the email address ends with (for ex: google.com, || anything i pass to regex)

So I need to pass a variable inside regex itself but i don't know how to do that 

Here is what i did ---> this worked fine but it still static every time i want different domain i must change it here

export const emailAddress = (message = Strings.validationEmail) => new Validator({
  key: 'EMAIL',
  message,
  validator: value => /^[a-z0-9](\.?[a-z0-9]){5,}@$gmail.com$/ig.test(`${value}`),
});

Now i want to replace google.com with any domain i provide to this function to be dynamic but the code below didn't work for me

export const emailAddressEndsWith = (domain = 'tvtc.com', message = Strings.validationEmailStartsWith) => new Validator({
  key: 'EMAIL',
  message: template(message)({ domain }),
  validator: value => new RegExp(`/^[a-z0-9](\.?[a-z0-9]){5,}@${domain}$/ig`).test(`${value}`),
});
  • Inside a `RegExp` constructor, you should not use a regex literal, use the pattern as the first argument, and flags as the second. See https://stackoverflow.com/a/4029142/3832970 – Wiktor Stribiżew Sep 24 '19 at 13:11
  • @WiktorStribiżew Could u please write the answer here for me because i read it but i couldn't undrstand – Mohammed Saber Mohammed Sep 24 '19 at 13:13
  • No need to, see a more specific [answer](https://stackoverflow.com/questions/43390873/template-literal-inside-of-the-regex), I have already answered such a question. – Wiktor Stribiżew Sep 24 '19 at 13:17

0 Answers0