javascript - Converting a Distance string into a Number -


when using google api calculating distance between 2 location, returns me distance in string format.

for example: 1,585 km, 54 km etc.

i want converted number. best way convert string 1,525 km number 1525?

if using google maps distance matrix api, there field in distance object called value has number value. documented in api, units metres.

but answer original question, remove non-digit characters string using regular expression:

var dist = '1,525 km'; dist = number(dist.replace(/[^\d]/g, '')); 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -