-1

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.

user3361996
  • 373
  • 3
  • 14

1 Answers1

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