[英]extracting string using javascript regex
Given: 鉴于:
"4 John Smith Rd., Pasadena 94786"
or "4 John Smith Rd., Pasadena 94786"
或
"4 John Smith Rd., North Pasadena 94786".
The following regex failed to extract the City in array index 0 and the zip code in array index. 以下正则表达式未能提取数组索引0中的City和数组索引中的邮政编码。
How to fix it? 如何解决? thx
谢谢
doc.address.match(/, (\\w+) +(\\d{5})/g);
var address = ["4 John Smith Rd., Pasadena 94786", "4 John Smith Rd., North Pasadena 94786" ] for(var i in address){ console.log(address[i].match(/, (.+)? +(\\d{5})/)); }
group1 is city and group2 is the zip code. group1是城市,group2是邮政编码。
If match
is successful, the captures will begin at the first index. 如果
match
成功,则捕获将从第一个索引开始。 Try: 尝试:
var result = docs.address.match(/, ([\w ]+) (\d{5})/i);
var city = result[1];
var zip = result[2];
But this is pretty naïve address parsing. 但这是非常幼稚的地址解析。 There are libraries dedicated to this.
有专门用于此的库。
This will match all after the .,
这将与
.,
之后的所有内容匹配
var rs = docs.address.match(/[^\.\,](\w+ ){1,}(\d+)/);
Dont match the .
不匹配
.
and ,
then match any word after it, then match the ZIP code 和
,
然后匹配其后的任何单词,然后匹配邮政编码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.