javascript - regular expression, extract coordinates from string -
javascript, want extract coordinates string using regex. string including space , new line,
127.518037,37.834511 127.518037,37.834511 127.518103,37.834808 127.518103,37.834808 127.518169,37.835209 127.518169,37.835209 127.518147,37.835558 127.518147,37.835558 127.518059,37.835750 127.518059,37.835750 127.518081,37.835976 127.518081,37.835976 127.518411,37.836412 127.518411,37.836412 127.518697,37.836761 127.518697,37.836761 127.518719,37.837198 127.518719,37.837198 127.518741,37.837669 127.518741,37.837669 127.518477,37.838087 127.518477,37.838087 127.518433,37.838401 127.518433,37.838401
i have tried this, result not want to.
var coords = item.split(/\s/); for( item in coords){ coords_list = item.split(/,/); }
coords_list result,
["127.518037,37.834511", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "127.518037,37.834511", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "127.518103,37.834808", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "127.518103,37.834808", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ...
any idea, help~
try split on:
/[\s,]+/
the character class [\s,]
matches white space character or comma, , +
tells match said class once or more. way don't end empty strings in split array.
or in 2 steps, first split on \s+
instead of single \s
:
the following:
var text = " 127.518037,37.834511\n" + " 127.518037,37.834511\n" + " 127.518103,37.834808\n" + " 127.518103,37.834808\n" + " 127.518169,37.835209\n" + " 127.518169,37.835209\n" + " 127.518147,37.835558\n" + " 127.518147,37.835558\n" + " 127.518059,37.835750\n" + " 127.518059,37.835750\n" + " 127.518081,37.835976\n" + " 127.518081,37.835976\n" + " 127.518411,37.836412\n" + " 127.518411,37.836412\n" + " 127.518697,37.836761\n" + " 127.518697,37.836761\n" + " 127.518719,37.837198\n" + " 127.518719,37.837198\n" + " 127.518741,37.837669\n" + " 127.518741,37.837669\n" + " 127.518477,37.838087\n" + " 127.518477,37.838087\n" + " 127.518433,37.838401\n" + " 127.518433,37.838401"; var coords = text.split(/\s+/); for(item in coords){ var coords_list = coords[item].split(/,/); print(coords_list); }
will print:
127.518037,37.834511 127.518037,37.834511 127.518103,37.834808 127.518103,37.834808 127.518169,37.835209 127.518169,37.835209 127.518147,37.835558 127.518147,37.835558 127.518059,37.835750 127.518059,37.835750 127.518081,37.835976 127.518081,37.835976 127.518411,37.836412 127.518411,37.836412 127.518697,37.836761 127.518697,37.836761 127.518719,37.837198 127.518719,37.837198 127.518741,37.837669 127.518741,37.837669 127.518477,37.838087 127.518477,37.838087 127.518433,37.838401 127.518433,37.838401
as can tested on ideone.
Comments
Post a Comment