gpt4 book ai didi

JavaScript : How to find the position of a zip code in a string?

转载 作者:行者123 更新时间:2023-11-28 19:27:04 26 4
gpt4 key购买 nike

我有一个这样的地址:7 CITE VANEAU - 75007 PARIS。我的目标是获取邮政编码。每个地址的邮政编码可能会发生变化,但长度始终为 6 位数字。我想找到字符串中邮政编码开头的位置。你有什么想法吗?

最佳答案

http://jsfiddle.net/jeffreyTang/8038whkt/

var re = /[0-9]{6}/; 
var str = '7 CITE VANEAU - 750007 PARIS';

// get the index of the dash
var dash = str.indexOf('-');

// remove everything before the dash
str = str.substring(dash);

// execute the pattern match
var m = re.exec(str);

// this is your answer
console.log(m[0]);

关于JavaScript : How to find the position of a zip code in a string?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27658625/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com