Search code examples
javascriptregexlatitude-longitudestring-parsing

In JavaScript, How to extract latitude and longitude from string


I am extracting a string from a database which needs to be parsed into latitude and longitude separately, The string is defined as a "point", in the following format:

(2.340000000,-4.50000000)

I am trying to remove the parenthesis, and then parsed them with the method split() but I haven't been able to come up with a regular expressions that does the job right:

So far I have tried many alternatives, and

var latlong = "(2.34000000, -4.500000000)"
latlong.replace('/[\(\)]//g','');
var coords = latlong.split(',');
var lat = coords[0];
var long = coords[1];

If I run that, all I got is: NaN, -4.500000

What am I doing wrong? Thanks!


Solution

  • Seems to work, but you had an extra slash

    var value = '(2.340000000,-4.50000000)';
    
    //you had an extra slash here
    //           ''''''''''''''''v
    value = value.replace(/[\(\)]/g,'').split(',');
    
    console.log(value[0]);
    console.log(value[1]);​