0

I have this regex to validate Swift BIC:

^[A-Z]{6}[A-Z0-9]{2}([A-Z0-9]{3})?

But this string is correct:

AABSDE31X-X

How would be the regex to match the entire optional part ([A-Z0-9]{3})? if present?

Thanks in advance.

Alavaros
  • 1,665
  • 7
  • 32
  • 52
  • What are the rules here? Any 3 alphanumeric + `-` after the 6 letters and 2 alphanumeric? Try [`^[A-Z]{6}[A-Z0-9]{2}(?:[-A-Z0-9]{3})?$`](https://regex101.com/r/dFKiWe/2). – Wiktor Stribiżew Oct 25 '17 at 12:48
  • @Wiktor your expression returns true for invalid code: `var code = "AABSDE31X-X"; /^[A-Z]{6}[A-Z0-9]{2}(?:[-A-Z0-9]{3})?$/.test(code)` https://en.wikipedia.org/wiki/ISO_9362 – Killzone Kid Oct 25 '17 at 13:49
  • @KillzoneKid Then it is another dupe that have been so many. – Wiktor Stribiżew Oct 25 '17 at 13:50

1 Answers1

2

Seems like you just have to append you regex with $ to terminate it :

^[A-Z]{6}[A-Z0-9]{2}([A-Z0-9]{3})?$

A great tool to check your regex here : https://regex101.com/

Hope this helps!

Philippe Sultan
  • 2,111
  • 17
  • 23