Search code examples
javascriptparsingcoordinatescoords

parse coordinates from text


I like to extract the coordinates from this text:

Text
492k | 505k auf Punkt
https://www.seite.com/map?ll=30.123456,21.123456
Text
83k | 1,8 Mio auf Punkt
https://www.seite.com/map?ll=11.146724,27.427684
Text
82k | 121k auf Punkt
https://www.seite.com/map?ll=24.142451,36.127474
Text
20k | 65k auf Punkt
https://www.seite.com/map?ll=26.241442,16.323624
Text
11k | 93 auf Punkt
https://www.seite.com/map?ll=47.139682,14.124675

I have tried but it did not work well: https://jsfiddle.net/pnyqrgfz/ The result should be like this:

30.123456,21.123456
11.146724,27.427684
24.142451,36.127474
26.241442,16.323624
47.139682,14.124675

Solution

  • What about this short solution:

    var source = 'https://www.seite.com/map?ll=30.123456,21.123456';
    var pattern = /[0-9]+\.[0-9]+/g;
    source.match(pattern);
    

    Output:

    ["30.123456", "21.123456"]
    

    RegEx example with the complete and long input:

    https://regex101.com/r/5FXAW8/1