I would like to find the alphanumeric canadian postal code from a string.
A string such as H9B2R1|taco|salsa|taco
or if encoded, H9B2R1%7Ctaco%7Csalsa%7Ctaco
.
The result I'm looking for is the trimmed postal code before any special characters and/or non-alphanumeric values.
How to I use split or regex to parse/match this to return H9B2R1
? I searched stackoverflow for a question like this, but didn't find any.
Asked
Active
Viewed 111 times
-1

user3361996
- 373
- 3
- 14
-
1Have you tried splitting with `|` and getting the first item? `"H9B2R1|taco|salsa|taco".split('|')[0]`? – Wiktor Stribiżew Jun 23 '21 at 23:34
-
@WiktorStribiżew, the selected answer was what I was looking for. – user3361996 Jun 24 '21 at 03:01
1 Answers
1
try this
"H9B2R1|taco|salsa|taco".match(/\w+/)[0] //returns H9B2R1
"H9B2R1%7Ctaco%7Csalsa%7Ctaco".match(/\w+/)[0] //returns H9B2R1
//or using split method
"H9B2R1|taco|salsa|taco".split(/\W/)[0] //returns H9B2R1
"H9B2R1%7Ctaco%7Csalsa%7Ctaco".split(/\W/)[0] //returns H9B2R1

Wai Ha Lee
- 8,598
- 83
- 57
- 92

Allure
- 513
- 2
- 12