如何使用正则表达式验证这些代码?
/^[a-z]{2,3}(?:-[A-Z]{2,3}(?:-[a-zA-Z]{4})?)?$/
下面是它的工作原理
^ <- Starts with [a-z] <- From a to z (lower-case) {2,3} <- Repeated at least 2 times,at most 3 (?: <- Non capturing group - <- The "-" character [A-Z] <- From a to z (upper-case) {2,3} <- Repeated at least 2 times,at most 3 (?: <- Non capturing group - <- The "-" character [a-zA-Z] <- from a to Z (case insensitive) {4} <- Repeated 4 times ) <- End of the group ? <- Facultative ) <- End of the group ? <- Facultative $ <- Ends here
您也可以用(?:-( ?: Cyrl | Latn))替换最后一个非捕获组?如果唯一的选择是Cyrl和Latn