开发者

regular expression, extract coordinates from string

javascript, I want to extract coordinates from a string using regex. this is my string including space and 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 like this, but the result is not what I 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 to split on:

/[\s,]+/

The character class [\s,] matches any white space character or a comma, and the + tells it to match said class once or more. That way you don't end up with empty strings in your split array.

Or do it in two steps, but first split on \s+ instead of a 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 be tested on IDEone.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜