I've got an address like this : 7 CITE VANEAU - 75007 PARIS. My goal is to get the zip code. The zip code can change for each address, but it's always 6 digits long. I want to find the position of the beginning of the zip code in the string. Do you have an idea ?
Asked
Active
Viewed 2,688 times
1
-
1Can you post what you have tried so far along with any research you've done to solve the problem yourself? – Anthony Forloney Dec 26 '14 at 15:01
-
is there always the char '-' with the spaces? if so myStrnig.indexOf(' - ') + 1 could to the job, although its not a nice solution.. – jony89 Dec 26 '14 at 15:04
-
No, I forgot to mention, sometimes there is more than a " - " symbol, so I can't use it. – Xavier C. Dec 26 '14 at 15:11
-
is it only for Paris? If yes, you can do `myStrnig.indexOf('75')` – Prerak Sola Dec 26 '14 at 15:13
-
You mention 6 digits, but the example has only 5 – Jeroen de Lau Dec 26 '14 at 15:18
-
No, it's not only Paris. But here what I've found : var zipcode = address.match(/(?:^|\D)(\d{5})(?=\D|$)/g)[0]; – Xavier C. Dec 26 '14 at 15:18
3 Answers
2
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]);

Jeff
- 2,293
- 4
- 26
- 43
2
Just use String.search
.
>> "7 CITE VANEAU - 750071 PARIS".search(/\d{6}/);
<< 16
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/search.
0
first find the zip code:
var arr = address.match(/(\d+)/g) ;
var zip ;
for (var i = 0 ;i < arr.length; i ++) {
if (arr[i].length ==6) zip = arr[i];
}
var pos = address.indexOf(zip)
this code can be simplified.

giannisf
- 2,479
- 1
- 17
- 29
-
Thanks, I've found this too : var zipcode = address.match(/(?:^|\D)(\d{5})(?=\D|$)/g)[0]; – Xavier C. Dec 26 '14 at 15:18
-
@JeroendeLau Yeah i forgot the length prop from arr. Also the op says that the zip is always 6 in length but the one provided is 5. – giannisf Dec 26 '14 at 15:34
-
I'm a little bit puzzled why this would be the accepted answer. It looks for multiple sequences of numbers in the string, even ones that could not be zip codes because they are too short. It uses redundant parentheses in the regexp. Then it laboriously checks each match for a length of six, even though that restriction could easily have been specified as part of the regexp. Then it has to use another call, to `indexOf`, to find the position, even though other string/regexp routines provide this for free. In my opinion this answer should be unaccepted so as not to mislead future visitors. – Dec 26 '14 at 15:46
-
In addition, this code will result in a run-time error if there is no match (it will try to access the `length` property of `null`). – Dec 26 '14 at 15:57
-
I removed the "accepted answer". Thank you for your advice on this question, it helped me a lot to understand the problem. – Xavier C. Dec 26 '14 at 18:45