Regex to Match Latitude and Longitude

Validate latitude and longitude coordinate pairs with this regex pattern. Checks valid ranges of lat -90 to 90 and lng -180 to 180. Free tool.

Regular Expression

/^-?(?:90(?:\.0+)?|[0-8]?\d(?:\.\d+)?),\s*-?(?:180(?:\.0+)?|(?:1[0-7]\d|\d{1,2})(?:\.\d+)?)$/

Token Breakdown

TokenDescription
^Anchors at the start of the string (or line in multiline mode)
-Matches the literal character '-'
?Makes the preceding element optional (zero or one times)
(?:Start of non-capturing group
9Matches the literal character '9'
0Matches the literal character '0'
(?:Start of non-capturing group
\.Matches a literal dot
0Matches the literal character '0'
+Matches the preceding element one or more times (greedy)
)End of group
?Makes the preceding element optional (zero or one times)
|Alternation — matches the expression before OR after the pipe
[0-8]Character class — matches any one of: 0-8
?Makes the preceding element optional (zero or one times)
\dMatches any digit (0-9)
(?:Start of non-capturing group
\.Matches a literal dot
\dMatches any digit (0-9)
+Matches the preceding element one or more times (greedy)
)End of group
?Makes the preceding element optional (zero or one times)
)End of group
,Matches the literal character ','
\sMatches any whitespace character (space, tab, newline)
*Matches the preceding element zero or more times (greedy)
-Matches the literal character '-'
?Makes the preceding element optional (zero or one times)
(?:Start of non-capturing group
1Matches the literal character '1'
8Matches the literal character '8'
0Matches the literal character '0'
(?:Start of non-capturing group
\.Matches a literal dot
0Matches the literal character '0'
+Matches the preceding element one or more times (greedy)
)End of group
?Makes the preceding element optional (zero or one times)
|Alternation — matches the expression before OR after the pipe
(?:Start of non-capturing group
1Matches the literal character '1'
[0-7]Character class — matches any one of: 0-7
\dMatches any digit (0-9)
|Alternation — matches the expression before OR after the pipe
\dMatches any digit (0-9)
{1,2}Matches between 1 and 2 times
)End of group
(?:Start of non-capturing group
\.Matches a literal dot
\dMatches any digit (0-9)
+Matches the preceding element one or more times (greedy)
)End of group
?Makes the preceding element optional (zero or one times)
)End of group
$Anchors at the end of the string (or line in multiline mode)

Detailed Explanation

This regex validates geographic coordinate pairs in the format latitude,longitude. Here is the token-by-token breakdown:

^ — Anchors the match at the start.

-? — Optional minus sign for negative latitudes (southern hemisphere).

(?:90(?:.0+)?|[0-8]?\d(?:.\d+)?) — Matches valid latitude values (-90 to 90):

  • 90(?:.0+)? matches exactly 90, optionally with decimal zeros (90.0, 90.00)
  • [0-8]?\d matches integers 0-89 (the optional [0-8] handles tens digit)
  • (?:.\d+)? adds optional decimal places

,\s* — Matches a comma followed by optional whitespace, separating latitude from longitude.

-? — Optional minus sign for negative longitudes (western hemisphere).

(?:180(?:.0+)?|(?:1[0-7]\d|\d{1,2})(?:.\d+)?) — Matches valid longitude values (-180 to 180):

  • 180(?:.0+)? matches exactly 180 with optional decimal zeros
  • 1[0-7]\d matches 100-179
  • \d{1,2} matches 0-99
  • (?:.\d+)? adds optional decimal places

$ — Anchors the match at the end.

This pattern validates coordinates like 40.7128,-74.0060 (New York), -33.8688,151.2093 (Sydney), and 0,0 (Gulf of Guinea). It ensures latitude stays within -90 to 90 and longitude within -180 to 180.

Example Test Strings

InputExpected
40.7128,-74.0060Match
-33.8688, 151.2093Match
91,180No Match
0,0Match
90,-181No Match

Try It — Interactive Tester

//
gimsuy
No matches found.
Pattern: 91 charsFlags: noneMatches: 0

Ctrl+Shift+C to copy regex

Customize this pattern →