-1

How do refactor single-line regular expression to multiline?

export const emailStrictAccounting = value =>
    // eslint-disable-next-line vue/max-len,no-control-regex
    value && !/(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/ig.test(value)
        ? 'error'
        : undefined;
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • The multiple line flag is `m`. For the rest your question is ambiguous. Could you edit your question and explain what the intended behaviour is with example inputs, and what you expect to happen instead? – trincot Mar 24 '23 at 11:26
  • What does it currently do or not do that you want to change? What have you tried so far? Right now you are just asking us to figure it out what you want and do the work. You need to show us what you've tried and we can help you. – Chris Barr Mar 24 '23 at 11:47

1 Answers1

0

Simply extract unreadable things to variables (and use \d instead of [0-9]). the following is written in pseudocode so syntax doesn't matter, but you'll need to re-escape all backslashes when putting them into JS strings:

class1 = [a-z0-9!#$%&'*+/=?^_`{|}~-]
class2 = [\x01-\x09\x0b\x0c\x0e-\x7f]
class3 = [\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]
class4 = [\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]
alnum = [a-z0-9]
aln_hyp = [a-z0-9-]
numbers = 25[0-5]|2[0-4]\d|[01]?\d\d?

(?:
  ${class1}+
  (?:\.${class1}+)*
|
  "(?:${class3}|\\${class2})*"
)
@
(?:
  (?:${alnum}(?:${aln_hyp}*${alnum})?\.)+
  ${alnum}(?:${aln_hyp}*${alnum})?
|
  \[
  (?:(?:${numbers})\.){3}
  (?:
    ${numbers}
  |
    ${aln_hyp}*
    ${alnum}:
    (?:${class4}|\\${class2})+
  )
  \]
)

P/S: If you are trying to validate emails, we have a whole debate on that.

InSync
  • 4,851
  • 4
  • 8
  • 30