I have this string
'john smith~123 Street~Apt 4~New York~NY~12345'
Using JavaScript, what is the fastest way to parse this into
var name = "john smith";
var street= "123 Street";
//etc...
I have this string
'john smith~123 Street~Apt 4~New York~NY~12345'
Using JavaScript, what is the fastest way to parse this into
var name = "john smith";
var street= "123 Street";
//etc...
With JavaScript’s
String.prototype.split
function:You can use
split
to split the text.As an alternative, you can also use
match
as followThe regex
[^~]+
will match all the characters except~
and return the matches in an array. You can then extract the matches from it.Use this code------
You'll want to look into JavaScript's substr or split as this is not really a task suited for jQuery
Even though this is not the simplest way, you could do this:
Update for ES2015, using destructuring