I have a string which contains location:
var string = "[ 30.733315, 76.77941799999999,City,6,10/28/2013 10:28:39 AM]"
How to convert this to an array, so that i can get
arr[0] = 30.733315,
arr[1] = 76.77941799999999 so on.
String.split()
is not working here.
Split on [
, ]
and ,
.
]
must be escaped in the split regex
var arr = string.split(/[[,\]]/).filter(function(el) { return el.trim(); })
Array.prototype.filter
is required to remove empty strings from the array.
A more readable version of filter
would be:
arr.filter(function(el) {
return el.trim().length === 0;
});
var string = "[ 30.733315, 76.77941799999999,City,6,10/28/2013 10:28:39 AM]";
string = string.replace('[',"['");
string = string.replace(']',"']");
string = string.replace(',',"','","gi");
console.log(string);
arr=eval(string);
console.log(arr);
Try this
var mystring= "[ 30.733315, 76.77941799999999,City,6,10/28/2013 10:28:39 AM]"
var list = mystring.split(',');
Output:
for(var i = 0; i < list.length; i++)
{
alert(list[i]);
}
String.split Splits a String object into an array of strings by separating the string into substrings.
A cross-browser solution:
var string = "[ 30.733315, 76.77941799999999,City,6,10/28/2013 10:28:39 AM]";
var arr = string.replace(/\[|\]/g,'')
.replace(/ /g,'')
.split(',');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.