开发者

Parse address out of string that includes html <BR>'s in javascript

I have an address thats in string that looks like this:

"1234 Something Street<br>Chicago, IL 34571<br>"

I'm having difficulty separating it out into the variables:

var street = ...;
var city = ...;
var state = ...;
var zip开发者_JAVA技巧 = ...;

Whats is a good way to do this in javascript?

Thanks!


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];

jsFiddle.

This new updated one will match Salt Lake City.


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];


you can do it multiple ways.

1234 Something Street
Chicago, IL 34571

first split it by br

arro[0]="1234 Something Street"

and the break by space...

keep doig this...until you get all values


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];
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜