从文本块检索城市/州/拉链线的问题

问题描述 投票:0回答:1

我有一堆文本块,其中包含地址信息,例如:

Fake Name
Business name
60 N Address Ave.
San Jose, CA 95126
(555)255-5555
Email: [email protected]

我需要提取包含城市/州/邮政编码的行,但它可以位于每个文本块中的不同行上。我尝试过使用正则表达式,但没有得到任何返回结果。任何帮助将不胜感激!

function findStateLine(text) {
    const lines = text.split("\n");

    const regex = /\b[A-Z]{2}\b/;

    for (let i = 0; i < lines.length; i++) {
        if (regex.test(lines[i])) {
            return lines[i].trim();
        }
    }

    return null;
}


const stateLine = findStateLine(text);
console.log(stateLine);

期待上面的行“San Jose, CA 95126”,但没有得到任何结果

还在正则表达式 const 中尝试了 '/[\w ]+, \w{2} \d{5}/' 但也没有得到结果。

javascript regex function
1个回答
0
投票

您可以使用此模式:

(?i)^\s*([^,\r\n]+)[,]?\s+([a-z]{2})\s+[0-9]{5}-?([0-9]{4})?\b
© www.soinside.com 2019 - 2024. All rights reserved.