Regular Expression for CultureInfo
What is the regular expression for en-NZ for the CultureInfo, is it /{2, [a-z]}-{2, [A-Z]} ?
I also need for the expression to check for Turkey, which is two lower case lette开发者_运维技巧rs. For example 'tr' which is the only language code with two letters.
Try this:
/\b[a-z]{2,3}(?:-[A-Z]{2,3}(?:-(?:Cyrl|Latn))?)?\b/
Explanation:
\b # word boundary
[a-z]{2,3} # 2-3 lowercase letters
(?: # Try to match the following:
-[A-Z]{2,3} # dash, 2-3 uppercase letters
(?: # Try to match...
- # dash
(?:Cyrl|Latn) # Cyrl or Latn
)? # optionally
)? # optionally
\b # word boundary
This matches all of these valid CultureInfo codes:
tr
tr-TR
syr
sr-SP-Latn
kok-IN
zh-CHT
Not sure exactly what your asking so here is a very good regex generation site.
http://www.txt2re.com/
Hope this helps.
If what you're looking for is a regex that finds two lowercase letters, followed by a dash, followed by two uppercase letters, then use:
[a-z]{2}-[A-Z]{2}
This may work.
/([a-z]{2}-[A-Z]{2}|[a-z]{2})/
Normal regex syntax would be
[a-z]{2}-[A-Z]{2}
but the problem with this is that it will also match en-AU (ugh, the horrors of it...), en-GB, en-US, fr-FR, etc.
What you can use instead to search for that specific value is this:
var re = /en-NZ/;
Here is the W3C tutorial on Javascript regexes, and this page also has a javascript regex tester (select the Client-side Engine in the top dropdown, and the JavaScript engine in the dropdown at the bottom of the page).
精彩评论