如果存在单元号,则正则表达式提取从门牌号开始的完整地址
regex experession to extract complete address starting from house number if unit numbers are present
如果给定字符串中存在单元号,我将尝试从门牌号开始提取地址。
如果单位编号不可用,select 整个字符串。
const adss1 = 'U 4 21 House Ave, Suburb State 2020';
const adss2 = 'U 14 21 House Ave, Suburb State 2020';
const adss3 = '21 House Ave, Suburb State 2020';
const regEx = /(([0-9]+)((\s+[a-zA-Z,]+|\s+[a-zA-Z,.]+\s){2,10})?(\#[0-9a-z-\-]+|\#\s+[0-9\-]+|[0-9\-]+))/g;
const match1 = adss1.match(regEx)
console.log(match1)
// [ '21 House Ave, Suburb State 2020' ]
const match2 = adss2.match(regEx)
console.log(match2)
//[ '14', '21 House Ave, Suburb State 2020' ]
const match3 = adss3.match(regEx)
console.log(match3)
//[ '21 House Ave, Suburb State 2020' ]
当前的正则表达式模式适用于 ads1 和 ads3 地址类型,但不适用于 ads2 变量。
寻找改进我的正则表达式模式的建议。谢谢
据我所知,人们只想跳过开头的所有内容,直到匹配空白(序列)后跟非数字字符之前的第一个数字字符;然后从那里继续匹配所有内容(直到结束)... /\d+\s+\D.*$/ ...
function extractAddress(data) {
return ((/\d+\s+\D.*/).exec(String(data)) || [''])[0];
}
console.log([
'U 4 21 House Ave, Suburb State 2020',
'U 14 21 House Ave, Suburb State 2020',
'21 House Ave, Suburb State 2020',
'hasvk 1223 21 House Ave, Suburb State 2020',
'has vk 12 23 21 House Ave, Suburb State 2020',
'12 23 21 House Ave, Suburb State 2020',
].map(extractAddress));
console.log(`U 4 21 House Ave, Suburb State 2020
U 14 21 House Ave, Suburb State 2020
21 House Ave, Suburb State 2020
hasvk 1223 21 House Ave, Suburb State 2020
has vk 12 23 21 House Ave, Suburb State 2020
12 23 21 House Ave, Suburb State 2020`.match(/\d+\s+\D.*$/gm));
.as-console-wrapper { min-height: 100%!important; top: 0; }
如果给定字符串中存在单元号,我将尝试从门牌号开始提取地址。
如果单位编号不可用,select 整个字符串。
const adss1 = 'U 4 21 House Ave, Suburb State 2020';
const adss2 = 'U 14 21 House Ave, Suburb State 2020';
const adss3 = '21 House Ave, Suburb State 2020';
const regEx = /(([0-9]+)((\s+[a-zA-Z,]+|\s+[a-zA-Z,.]+\s){2,10})?(\#[0-9a-z-\-]+|\#\s+[0-9\-]+|[0-9\-]+))/g;
const match1 = adss1.match(regEx)
console.log(match1)
// [ '21 House Ave, Suburb State 2020' ]
const match2 = adss2.match(regEx)
console.log(match2)
//[ '14', '21 House Ave, Suburb State 2020' ]
const match3 = adss3.match(regEx)
console.log(match3)
//[ '21 House Ave, Suburb State 2020' ]
当前的正则表达式模式适用于 ads1 和 ads3 地址类型,但不适用于 ads2 变量。
寻找改进我的正则表达式模式的建议。谢谢
据我所知,人们只想跳过开头的所有内容,直到匹配空白(序列)后跟非数字字符之前的第一个数字字符;然后从那里继续匹配所有内容(直到结束)... /\d+\s+\D.*$/ ...
function extractAddress(data) {
return ((/\d+\s+\D.*/).exec(String(data)) || [''])[0];
}
console.log([
'U 4 21 House Ave, Suburb State 2020',
'U 14 21 House Ave, Suburb State 2020',
'21 House Ave, Suburb State 2020',
'hasvk 1223 21 House Ave, Suburb State 2020',
'has vk 12 23 21 House Ave, Suburb State 2020',
'12 23 21 House Ave, Suburb State 2020',
].map(extractAddress));
console.log(`U 4 21 House Ave, Suburb State 2020
U 14 21 House Ave, Suburb State 2020
21 House Ave, Suburb State 2020
hasvk 1223 21 House Ave, Suburb State 2020
has vk 12 23 21 House Ave, Suburb State 2020
12 23 21 House Ave, Suburb State 2020`.match(/\d+\s+\D.*$/gm));
.as-console-wrapper { min-height: 100%!important; top: 0; }