const regex = /(?<lat0>\d+)[-|\s](?<lat1>\d+)[.|,|\s](?<lat2>\d+)['|\s]?(?<latDir>[N|n|S|s])[,|\s|\-|–]+(?<lon0>\d+)[-|\s](?<lon1>\d+)[.|,|\s](?<lon2>\d+)['|\s]?(?<lonDir>[E|e|W|w])/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(?<lat0>\\d+)[-|\\s](?<lat1>\\d+)[.|,|\\s](?<lat2>\\d+)[\'|\\s]?(?<latDir>[N|n|S|s])[,|\\s|\\-|–]+(?<lon0>\\d+)[-|\\s](?<lon1>\\d+)[.|,|\\s](?<lon2>\\d+)[\'|\\s]?(?<lonDir>[E|e|W|w])', 'gm')
const str = `48-22.676N, 004-27.209W
47-14.13'N, 002-16.32'W
48 45,852N - 003 07,217W
44-39.95'N, 001-09.95'W
45-54.35n, 001-19.37w
47-38.542N - 003-14.699W
47-34.39 N – 002-52.41W
47 53 823 N – 3 58 538 W`;
// Reset `lastIndex` if this regex is defined globally
// regex.lastIndex = 0;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions