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!
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]);
You can cut out the split and just use the match function with the regex \((\-?\d+\.\d+), (\-?\d+\.\d+)\) which will return the two coordinates. Firebug console output:
>>> "(2.34000000, -4.500000000)".match(/\((\-?\d+\.\d+), (\-?\d+\.\d+)\)/);
["(2.34000000, -4.500000000)", "2.34000000", "-4.500000000"]
var latlong = "(2.34000000, -4.500000000)"
var coords = latlong.replace(/[\(\) ]/g,'').split(',');
console.log(coords[0])
console.log(coords[1])
You can try to use match function:
var latlong = "(2.34000000, -4.500000000)"
var coords = latlong.match(/\((-?[0-9\.]+), (-?[0-9\.]+)\)/);
var lat = coords[1];
var long = coords[2];
alert('lat: ' + lat);
alert('long: ' + long);
Related
I have a string like (12.131883, -68.84942999999998) and with using .replace() I wish to remove the brackets, or get the values between the brackets. A simple latlon = latlon.replace('(',' '') is not working.
Also tried it with latlon.replace(/\(.*?\)\s/g, '') but no luck.
Can anyone help me out here?
You can use substring:
var myString = "(12.131883, -68.84942999999998)";
var latlong = myString.substring(1, myString.indexOf(')'));
Or:
var myString = "(12.131883, -68.84942999999998)";
var latlong = myString.substring(myString.indexOf('(') + 1, myString.indexOf(')'));
You can get them in an array with:
var latlon = "(12.131883, -68.84942999999998)";
var ll = latlon.match(/[-+]?[0-9]*\.?[0-9]+/g)
console.log(ll) // returns ["12.131883", "-68.84942999999998"]
I'd like parse the float numbers of an array how this:
var array = "-51.2132,0.3100";
I tried with match(/\d+/g) but I'd like to take float
Any idea about the regex
Thanks in advance
Regex isn't required here. You can first split the coordinates by , to get the values, the use ParseFloat to cast them. Try this:
var loc = "-51.2132,0.3100".split(',');
var lat = parseFloat(loc[0]); // = -51.2132
var lon = parseFloat(loc[1]); // = 0.31
Try this:
var floats = array.split(',').map(function(e){return parseFloat(e)});
// result:
[-51.2132, 0.31]
What this line does: first, split the array on the comma character:
array.split(',') // ["-51.2132", "0.3100"]
Then, replace each item in that array with a parseFloat(item):
["-51.2132", "0.3100"].map(function(e){ // For each item in the array
return parseFloat(e); // Cast the current value to a float.
}); // [-51.2132, 0.31]
(-?\d+(?:\.\d+)?)
Try this.Grab the match.See demo.
http://regex101.com/r/dZ1vT6/43
Try this one:
var array = "-51.2132,0.3100";
var regex = /-?\d+\.?\d*/g;
var items = array.match(regex);
var numbers = items.map(function (item) {
return parseFloat(item);
});
http://regexr.com/39o6i
I have for example:
var locations = new google.maps.LatLng('#item.latitude', '#item.longitude', false);
#item.latitude value is for example: "43,321"
but i need it to be: 43.321
I need to force decimal separator as point not comma. How can I do this?
try this:
var lat = '#item.latitude';
var lng = '#item.longitude';
var locations = new google.maps.LatLng(lat.replace(',','.'), lng.replace(',','.'), false);
How do you replace parenthesis with javascript.
I have a format like this:
(14.233,72.456),(12.4566,45.345),(12.456,13.567)
How can I get a format like given below:
14.233,72.456#12.4566,45.345#12.456,13.567
I have tried the following:
bounds = bounds.replace(/\)\,\(/g,'#');
bounds = bounds.replace(/\(/g,'');
bounds = bounds.replace(/\)/,'');
Splitting the string up by the delimiters and join them with your new delimiter:
var data = "(14.233,72.456),(12.4566,45.345),(12.456,13.567)";
data = data.slice(1, -1).split('),(').join('#');
Or using RegEx:
var data = "(14.233,72.456),(12.4566,45.345),(12.456,13.567)";
data = data.slice(1, -1).replace(/\),\(/g, '#');
You may try this (matches only float numbers) :
var s = '(14.233,72.456),(12.4566,45.345),(12.456,13.567)';
bounds = s.match(/\d+\.\d+,\d+\.\d+/g).join('#');
s.match(/\d+\.\d+,\d+\.\d+/g) returns :
['14.233,72.456', '12.4566,45.345', '12.456,13.567']
In addition, you might need to deal with an empty string :
bounds = (s.match(/\d+\.\d+,\d+\.\d+/g) || []).join('#');
var string = "(14.233,72.456),(12.4566,45.345),(12.456,13.567)";
var str = string.substring(1, string.length-1).split('),(').join('#');
alert(str);
Try this, which is almost the same as your code:
bounds = bounds.replace(/\),\(/g,'#').replace(/^\(|\)$/g,'');
See here the code working: http://jsfiddle.net/K8ECj/
[Edited to eliminate capture]
You can use .replace("(", "").replace(")", "");
I have this:
var result = "(55.6105023, 12.357556299999942)";
I would like to remove the brackets, and i thought that you could do this with substr (remove the first and last char in the string) <- But i can only manage to remove the last char if i know the length.
And then i would like to put the first number 55.6105023 into variable lat and the second 12.357556299999942 to variable lng by using explode() <- but this does not exists in JS
How can i do this?
Use slice(), which can take negative indices and unlike substr() is standardized and works in all browsers:
result.slice(1, -1);
You can use split() for the other part:
var parts = result.slice(1, -1).split(", ");
var lat = parts[0], lng = parts[1];
Alternatively you could use a regex:
var res = /^\(([\d.]+),\s*([\d.]+)\)$/.exec(result);
var lat = res[1], lng = res[2];
Remove the first and last characters by using substr, replace or slice. Use split to split the string into an array on the comma. Like so:
var chunks = result.slice(1, -1).split(", ");
var lat = chunks[0].trim();
var lng = chunks[1].trim();
I've added the trim to make sure all whitespaces are gone.
You can use a regular expression to pull out the numbers:
"(55.6105023, 12.357556299999942)".match(/[\d\.-]+/g);
// returns ["55.6105023", "12.357556299999942"]
The regex will match digits, decimal points and minus signs.
var result = "(55.6105023, 12.357556299999942)";
var substring = result.substring(1, result.length - 1);
var coordinates = substring.split(",");
var lat = coordinates[0];
var lng = coordinates[1];
You can use string manipulation functions like split and slice to parse out the numbers (or even a regular expression with matching groups), and the Number constructor to parse a number from a string:
var parseResult = function(s) {
var ss = s.split(', ');
return [Number(ss[0].slice(1)), Number(ss[1].slice(0, -1))];
};
var x = parseResult('(55.6105023, 12.357556299999942)');
x[0]; // => 55.6105023
x[1]; // => 12.357556299999942
You can do it by using substring and indexOf:
var result = "(55.6105023, 12.357556299999942)";
var lat = result.substring(result.indexOf('(')+1, result.indexOf(','));
var lon = result.substring(result.indexOf(', ')+1, result.indexOf(')'));
alert(lat); // alerts 55.6105023
alert(lon); // alerts 12.357556299999942
Here is another way, which first converts the data into proper JSON and then into a JavaScript array:
str = str.replace('(', '[').replace(')', ']');
var data = JSON.parse(str),
lat = data[0],
lng = data[1];