Skip to content
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

PartMeaning
^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.4194
  • 0, 0
  • 90.0, 180.0
  • -90, -180
  • 51.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')))               # False

Common Pitfalls

  • Decimal precision is flexible — 37.7749 (4 decimal places) and 51.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"W is completely different and requires a separate pattern
  • Latitude-first convention (lat, lng) is standard in geographic systems, but some APIs use lng, 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 — - 90 is 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

What about coordinates in Degrees-Minutes-Seconds (DMS) format?
DMS format (e.g., 37°46'29"N, 122°25'09"W) requires a completely different pattern that validates degrees (0–90 or 0–180), minutes (0–59), seconds (0–59), and directional indicators (N/S/E/W). Consider using a dedicated DMS parser rather than a regex for production systems.
Is latitude always written first?
In most geographic standards (GeoJSON, ISO 6709), latitude comes first. However, some mapping APIs use longitude-first order (Google Maps API uses lng, lat in certain contexts). Always verify the expected order of the system you are integrating with.

Related Patterns

Regex for Numbers Regex for URL

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro