Search code examples
javascriptstringtypeof

Javascript typeof string latlng address


var str = "40.697299, -41.817628";
var str = "10 Downing Street, London";
var str = "10 Downing Street";

I'm trying to figure out how to differentiate between the three above. I tried typeof but this all output as string so I was wondering is there a way using Javascript to pick up if the above is an address or latlng and return it?


Solution

  • These are both strings so you will have to look at the contents of them. I'd suggest using a regular expression, such as

    function isLatLng(str) {
      return str.match(/^(\-?\d+(\.\d+)?),\s*(\-?\d+(\.\d+)?)$/) !== null;
    }
    

    This will return true if it is a latitude/longitude