I have a sample string:
iasfa345.12isnhf Lat: 46.7132N, Long: 116.997W EFONA345.55SDF
I need to extract 116.997 and 46.7132 from it.
I only managed to get:
x.match(/Long: .{7}/g)
x.match(/Lat: .{6}/g)
But it includes Long: or Lat: that I need to substr() later.
The perfect solution would be to match any digit and a single dot between "Lat: " and "N or S" and between "Long: " and "W or E"
You could use the exec[MDN] method:
var str = 'iasfaisnhf Lat: 46.7132N, Long: 116.997W EFONASDF',
latPattern = /Lat: (\d*\.\d+)/,
longPattern = /Long: (\d*\.\d+)/;
alert(latPattern.exec(str)[1]);
alert(longPattern.exec(str)[1]);
If you need the direction indicators, which it seems like you would, the patterns would be:
/Lat: (\d*\.\d+)(N|S)/
/Long: (\d*\.\d+)(E|W)/
The direction indicators would be at index 2 of the object returned by exec.
Here's a working example.
For this format you can use this:
var str = "iasfaisnhf Lat: 46.7132N, Long: 116.997W EFONASDF";
var values = str.replace(/[^\d.,]/g, "").split(',');
values[0] == "46.7132"
values[1] == "116.997"
Live DEMO
Lat:\s?(.*?)N
Extract the first group and it will contain the latitude.
Long:\s?(.*?)W
Extract the first group and it will contain the longitude.
Related
Here is my String
Hey, Mounty Camp booking for {product} is Confirmed.%0A%0AHere are the details:%0ACamp Name: {product}%0ALocation: {location}%0AType: {roomType}%0ACheckIn : {checkIn}%0ACheckOut : {checkOut}%0AGuests: {guests}%0AAddress: {address}%0AGoogle Maps Location: {mapLink}%0A%0ANet Amount: {netAmount}%0AAdvance: {advanceAmount}%0APay at Camp: {pendingAmount}
Now I need the only middle of {} these two in an array
Here I have tried by this /{(.*?)} regular expressing but it is not working. Only it is giving the first middle part not entirely what I want.
Add g (global) flag to regex and after use this code :
var str = '%0A%0AHere are the details:%0ACamp Name: {product}%0ALocation: {location}%0AType: {roomType}%0ACheckIn : {checkIn}%0ACheckOut : {checkOut}%0AGuests: {guests}%0AAddress: {address}%0AGoogle Maps Location: {mapLink}%0A%0ANet Amount: {netAmount}%0AAdvance: {advanceAmount}%0APay at Camp: {pendingAmount}';
var result = str.match(/{(.*?)}/g).map(function (val) {
val = val.replace(/{/g, '');
return val.replace(/}/g, '');
});
console.log(result);
I get a JSON response like this from a server,
{
id : 1,
text : 'Name City Country \nJohn \n Chicago \nIllinois \nAlbert \nHouston \nTexas '
}
if I do console.log(response.text); the output is like this in tabular form
Now I want only want the Name column along with the rows and the output should look like this
Suggest me a workaround for this. Since the value is a string I'm facing a lot of trouble to extract only the required columns
I haven't tested this, but something like this should work:
var jsonData = "{ id : 1, text : 'Name City Country \nJohn \n Chicago \nIllinois \nAlbert \nHouston \nTexas ' }";
var obj1 = jsonData.split('\n');
for(var i= 1; i<obj1.length-2; i=i+3)
{
console.log("Name: " +obj1[i]+", City: "+obj1[i + 1]+", Country: " +obj1[i + 2]);
}
Use trim and split to get the Name City and Country texts.
const obj = { id : 1, text : 'Name City Country \nJohn \n Chicago \nIllinois \nAlbert \nHouston \nTexas ' }
console.log(obj.text.split("\n")[0].trim().split(" "));
You can use any of the javascript csv parser to parse the text to csv and access the particular column.
Ref: javascript-code-to-parse-csv-data
I am currently implementing google places autocomplete and the module I am using in React Native gives me the address as a whole string and not in address components. However, I need to have the postal code and city separate. The example response always look like this:
address: 'Calle Gran Vía, 8, 28013 Madrid, Spain
From this string I would need to have an object that looks like this:
{
city: 'Madrid',
postal_code: 28013,
}
How could I achieve this?
It's not the most "clean" or "smooth" answer, but it's something:
var response = "address: 'Calle Gran Vía, 8, 28013 Madrid, Spain";
var subStr = response.split(",")[2];
var obj = {
city: subStr.split(" ")[2],
postal_code: subStr.split(" ")[1]
};
console.log(obj);
For the city I think the best way is to use an array of cities and search it in the string
var str = "Calle Gran Vía, 8, 28013 Madrid, Spain";
var cities = ["Paris", "Berlin", "Madrid"];
var city = cities.filter(function(item) {
if (str.search(item) != -1)
return item;
})[0] || null;
For the postal code you should use a regex depending on the country (a good list of regex by country)
Probably split the string by ',' with array methods, take the third element of the array and split that by ' ', then you have your data points.
If you can always count on it being in that same format, you can do the following.
var splitAdress = address.split(",");
//This will give you ["Calle Gran Vía", " 8", " 28013 Madrid", " Spain"]
splitAdress = splitAdress[2].split(" ");
//This will give you ["", "28013", "Madrid"]
You'll first split the string into an array based on the comma and then follow it up by splitting on the space. The extra element in the second array is due to the space. This is an example of what #CBroe pointed out in the comments.
list=adress.split(",")[2].split()
list[0] gives you the postal code
list[1] gives you the city name
It depend on if there is always a comma in the "Calle Gran Vía, 8", if not you can use instead list=adress.split(",")[-2].split()
You might want to try this.
var address="Calle Gran Vía, 8, 28013 Madrid, Spain";
var splits = address.split(',')[2].trim().split(' ');
var newAdd = {
city : splits[1],
postal_code : splits[0]
}
console.log(newAdd);
i want to convertstring to latlng suppose string is 23.097278,72.446139 i want to convert it to latlng, when i split strings i get result as i want but when i converting to latlng it appends some digits at end of co ordinate
for example
centerPoint : (23.081295, 72.49794399999996), what i have done so far is below plz help me to solve it.
temp="23.097278,72.446139";
let temp1 = temp.split(',');
let coordinatesLat = temp1[0];
let coordinatesLong = temp1[1];
console.log("temp1 : " +coordinatesLong);
let centerPoint = new google.maps.LatLng(coordinatesLat, coordinatesLong);
console.log("centerPoint : " +centerPoint);
output in log
temp1 : 72.497944
centerPoint : (23.081295, 72.49794399999996)
Try to convert it to float
var latDouble = parseFloat(lat);
var longDouble = parseFloat(long);
I have an address string which varies from address to address.
var address_string = '6-3-54/A/889, Road no 45, Toli Chowki, Bangalore, Karnataka 700065, India'
How can I separate the following fields from the above address_string using jQuery
6-3-54/A/889
Road no 45
Toli Chowki
Bangalore
Karnataka
700065
India
Why jQuery?
what_you_need = address_string.split(', ')
You can use split function as below.
var arr = address_string.split(',');
Access and print as below:
for(var i=0;i<arr.length;i++)
{
document.write(arr[i]);
}
Please refer this link ( JS Fiddle) will help to solve you problem
You can use below code to get numbers(array) from String :
String.prototype.getNums= function(){
var rx=/[+-]?((\.\d+)|(\d+(\.\d+)?)([eE][+-]?\d+)?)/g,
mapN= this.match(rx) || [];
return mapN.map(Number);
};
var s= 'I want to extract 123 and 456 from this string ';
alert(s.getNums());
You can check Length as below :
alert(s.getNums()[0].toString().length);
// and so on