# javascript .match to check for right coordinate format

I am using javascript .match to check and correct right coordinate format after user writes it in textbox.

Working example:
input: lat=57.000000, long=24.000000
code: match(/(\d{2}).(\d{4,9})/gi)
returns: 57.000000,24.000000

How to write .match algoritme for MGRS coordinate:
input: 35VLD6787561669, mgrs=35VLD6787561669, etc.
code: ?
returns: 35VLD6787561669 (sequence: 2 numbers, 3 letters, 10 numbers)

According to the United States Naval Academy the Military Grid Reference System consists of:

• the UTM zone (1-60)
• a latitude band (C-X, excluding I and O; A-B and Y-Z are used for North and South Pole in the polar stereographic projection)
• A two-letter 100K grid square
• 1-5 digits easting
• 1-5 digits northing

The easting and northing define the accuracy and always come in even pairs (2, 4, 6, 8 or 10 digits). The above leads me to the following Regular Expression (Regex) (tested with https://regex101.com/):

### Solution

/^\d{1,2}[^IO]{3}(\d{10}|\d{8}|\d{6}|\d{4}|\d{2})\$/im

### Explanation

• /.../im denotes a Regex in Javascript with case-insensitive and multi-line flags (the latter needed for circumflex and dollar sign to work)
• ^...\$ circumflex and dollar sign stand for the start and end of the string (otherwise the Regex matches incomplete strings)
• \d{1,2}one or two digits
• [^IO] all characters except I and O. If you want to exclude North and South Pole, define [^ABIOYZ]
• (\d{2}|\d{4}|\d{6}|\d{8}|\d{10}) 2 digits or 4 or 6 or 8 or 10
• this should be marked as the answer – FPcond Jan 6 at 19:41

match(/\d{2}[A-Z]{3}\d{10}/gi)

\d{2} digits (0-9) (2 times)
[A-Z]{3} any character of: 'A' to 'Z' (3 times)
\d{10} digits (0-9) (10 times)
/gi ignore case