Regex for Coordinates (Latitude/Longitude) — Pattern Explained with Examples
Regex for Coordinates (Latitude/Longitude) — Pattern Explained with Examples
DodaTech
Updated Jun 20, 2026
2 min read
Geographic coordinate validation is essential for mapping applications, geocoding APIs, and location-based services. This pattern validates latitude and longitude pairs in decimal degrees format, enforcing the correct ranges: -90 to 90 for latitude and -180 to 180 for longitude.
The Pattern
/^-?([0-8]?\d(\.\d+)?|90(\.0+)?),\s*-?(1[0-7]\d(\.\d+)?|180(\.0+)?|[0-9]?\d(\.\d+)?)$/Pattern Breakdown
| Part | Meaning |
|---|---|
^ | Start-of-string anchor |
-? | Optional negative sign for latitude |
([0-8]?\d(\.\d+)?|90(\.0+)?) | Latitude: 0–89 with decimals, or 90.0 exactly |
, | Comma separator between lat and lng |
\s* | Optional whitespace after comma |
-? | Optional negative sign for longitude |
(1[0-7]\d(\.\d+)?|180(\.0+)?|[0-9]?\d(\.\d+)?) | Longitude: 0–179 with decimals, 180.0 exactly, or 0–99 |
$ | End-of-string anchor |
Matches
37.7749, -122.41940, 090.0, 180.0-90, -18051.5074, -0.1278
Does NOT Match
91, 0(lat > 90)0, 181(lng > 180)90.1, 0(lat exceeds 90)-90.01, 0(lat below -90)abc, 123
Language Examples
JavaScript
const coordRegex = /^-?([0-8]?\d(\.\d+)?|90(\.0+)?),\s*-?(1[0-7]\d(\.\d+)?|180(\.0+)?|[0-9]?\d(\.\d+)?)$/;
console.log(coordRegex.test('37.7749, -122.4194')); // true
console.log(coordRegex.test('91, 0')); // false
Python
import re
pattern = r'^-?([0-8]?\d(\.\d+)?|90(\.0+)?),\s*-?(1[0-7]\d(\.\d+)?|180(\.0+)?|[0-9]?\d(\.\d+)?)$'
print(bool(re.match(pattern, '37.7749, -122.4194'))) # True
print(bool(re.match(pattern, '91, 0'))) # FalseCommon Pitfalls
- Decimal precision is flexible —
37.7749(4 decimal places) and51.5(1 decimal place) both match, but remember that precision affects location accuracy (1 decimal place ≈ 11 km, 4 places ≈ 11 m) - Degrees-Minutes-Seconds (DMS) format like
37°46'29"N, 122°25'09"Wis completely different and requires a separate pattern - Latitude-first convention (
lat, lng) is standard in geographic systems, but some APIs uselng, lat— confirm the order expected by your data source - The comma separator is required between latitude and longitude values
- The negative sign must immediately precede the number without a space —
- 90is not valid
Real-World Use Cases
- Geocoding API integration — validate user-entered coordinates before sending them to a geocoding service
- Map marker placement — ensure coordinates fall within valid ranges before rendering on a map
- GPS data logging — validate coordinate strings from GPS devices before storing in a database
FAQ
Related Patterns
Regex for Numbers Regex for URL
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro