【问题标题】:Extract latest zip code from string who contain multiple zip code从包含多个邮政编码的字符串中提取最新的邮政编码
【发布时间】:2023-02-21 23:29:35
【问题描述】:

我想从一个可以包含多个 5 位数字的字符串中提取最新的邮政编码(5 位数字)。实际上我只能提取前 5 位数字。

var add =  '15 castle street BP 44210 41000 NewYork xxx';
var regex = /\d{5}/;
var result = add.match(regex);
console.log(result);

结果我得到 [44210],我希望 [41000]。有时字符串只包含 1 个邮政编码。

【问题讨论】:

    标签: javascript


    【解决方案1】:

    将全局标志添加到正则表达式。

    let str = '15 castle street BP 44210 41000 NewYork xxx';
    let regex = /d{5}/g;
    console.log(str.match(regex));

    【讨论】:

    • 奖励:也适用于 ZIP+4 格式。
    猜你喜欢
    • 2011-04-11
    • 2014-01-31
    • 2020-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多