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
| Token | Description |
|---|---|
| ^ | 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 |
| 9 | Matches the literal character '9' |
| 0 | Matches the literal character '0' |
| (?: | Start of non-capturing group |
| \. | Matches a literal dot |
| 0 | Matches 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) |
| \d | Matches any digit (0-9) |
| (?: | Start of non-capturing group |
| \. | Matches a literal dot |
| \d | Matches 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 ',' |
| \s | Matches 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 |
| 1 | Matches the literal character '1' |
| 8 | Matches the literal character '8' |
| 0 | Matches the literal character '0' |
| (?: | Start of non-capturing group |
| \. | Matches a literal dot |
| 0 | Matches 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 |
| 1 | Matches the literal character '1' |
| [0-7] | Character class — matches any one of: 0-7 |
| \d | Matches any digit (0-9) |
| | | Alternation — matches the expression before OR after the pipe |
| \d | Matches any digit (0-9) |
| {1,2} | Matches between 1 and 2 times |
| ) | End of group |
| (?: | Start of non-capturing group |
| \. | Matches a literal dot |
| \d | Matches 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
| Input | Expected |
|---|---|
| 40.7128,-74.0060 | Match |
| -33.8688, 151.2093 | Match |
| 91,180 | No Match |
| 0,0 | Match |
| 90,-181 | No Match |
Try It — Interactive Tester
91 charsFlags: noneMatches: 0Ctrl+Shift+C to copy regex