正则表达式 – 如何使用正则表达式验证文化代码?

前端之家收集整理的这篇文章主要介绍了正则表达式 – 如何使用正则表达式验证文化代码?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我真的不明白正则表达式,我也找不到任何正则表达式来验证文化代码:en-GB,en-UK,az-AZ-Cyrl等.

如何使用正则表达式验证这些代码

您可以验证:
/^[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

原文链接:https://www.f2er.com/regex/357201.html

猜你在找的正则表达式相关文章