0

The following two regular expressions are valid ones:

Email validation
[a-zA-Z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$…-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?

 UK Post code validation
^([Bb][Ff][Pp][Oo] ?[0-9]{1,3})$|^(([Gg][Ii][Rr] 0…hJ-Yj-y][0-9]?[A-Za-z]) ?)))[0-9][A-Za-z]{2} ?))$

However, when putting them into Angular Reactive forms:

address: this.fb.group({
                 previousPostCode: ["",
                [
                    Validators.required,
                    Validators.minLength(5),
                    Validators.maxLength(8),
                    Validators.pattern(/^([Bb][Ff][Pp][Oo] ?[0-9]{1,3})$|^(([Gg][Ii][Rr] 0…hJ-Yj-y][0-9]?[A-Za-z]) ?)))[0-9][A-Za-z]{2} ?))$/)
                ]
            ],
})

I am getting the following error: Error parsing regular expression: Invalid regular expression: /^([Bb][Ff][Pp][Oo] ?[0-9]{1,3})$|^(([Gg][Ii][Rr] 0…hJ-Yj-y][0-9]?[A-Za-z]) ?)))[0-9][A-Za-z]{2} ?))$/: Unmatched ')' . I am using VS 2017. Any idea? I am mainly interested in the post code validation.

Unknown developer
  • 5,414
  • 13
  • 52
  • 100
  • That's because you have 4 `)` more than you should. What are you trying to match? Might be easier to actually rewrite a regex pattern. You have another potential mistake in your pattern, a missing `[` before `0…` – ctwheels Jan 05 '18 at 15:14
  • 1
    Might be worth mentioning that [Validators.email](https://angular.io/api/forms/Validators#email) was added in v4 – hayden Jan 05 '18 at 15:26
  • Error in the email regex: `[a-zA-Z0-9!#$%&'*+\/=?^_\`{|}~-]+ (?: \. [a-zA-Z0-9!#$…-9] )? \. = ) <-- Unbalanced ')' + [a-zA-Z0-9] (?: [a-zA-Z0-9-]* [a-zA-Z0-9] )? ` –  Jan 05 '18 at 17:08
  • Error in postal code regex: `^ ( [Bb] [Ff] [Pp] [Oo] [ ]? [0-9]{1,3} ) # (1) $ | ^ ( # (2 start) ( [Gg] [Ii] [Rr] [ ]0…hJ-Yj-y] [0-9]? [A-Za-z] ) # (3) [ ]? ) # (2 end) = ) <-- Unbalanced ')' = ) <-- Unbalanced ')' [0-9] [A-Za-z]{2} [ ]? = ) <-- Unbalanced ')' = ) <-- Unbalanced ')' $` –  Jan 05 '18 at 17:09

0 Answers0