我的字符串中的地址如下所示:
"1234 Something Street<br>Chicago, IL 34571<br>"
我很难将它分成变量:
var street = ...;
var city = ...;
var state = ...;
var zip = ...;
什么是在javascript中执行此操作的好方法?
谢谢!
答案 0 :(得分:4)
var matches = address.match(/^(.+?)<br>([\w ]+),\s?(\w{2})\s+(\d{5})<br>$/);;
var street = matches[1],
city = matches[2],
state = matches[3],
zip = matches[4];
这个新的更新版本将匹配盐湖城。
答案 1 :(得分:3)
var sourceString = "1234 Something Street<br>Chicago, IL 34571<br>"
var arr = sourceString.split("<br>")
var street = arr[0];
var arr2 = arr[1].split(" ");
var city = arr2[0].substr(0, arr2[0].length - 1); // Strip the ","
var state = arr2[1];
var zip = arr2[2];
答案 2 :(得分:1)
你可以采取多种方式。
1234 Something Street
芝加哥,IL 34571
首先将其拆分为 br
arro[0]="1234 Something Street"
和空间的突破......
保持这个...直到你得到所有的价值
答案 3 :(得分:0)
str = "1234 Something Street<br>Chicago, IL 34571<br>";
res = str.replace(/,\s/ , "<br>").split("<br>");
street = res[0];
city = res[1];
state = res[2].split(' ')[0];
zip = res[2].split(' ')[1];