Skip to content

Instantly share code, notes, and snippets.

@peyerluk
Last active September 20, 2020 14:05
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peyerluk/8357036 to your computer and use it in GitHub Desktop.
Save peyerluk/8357036 to your computer and use it in GitHub Desktop.
Format swiss phone number
# validating samples:
# 783268674
# 0783268674
# 0410783268674
# 00410783268674
# +410783268674
# 041783268674
# 0041783268674
# +41783268674
# ++41783268674
# ++41(0)783268674
# (0)783268674
swissPhoneRegex: /^(0041|041|\+41|\+\+41|41)?(0|\(0\))?([1-9]\d{1})(\d{3})(\d{2})(\d{2})$/
# swissPhoneRegex is setup so we can also use it to format valid phone numbers consistently
formatPhoneNumber: (phone) ->
# regex is set up so we can easily format the matching parts
phone = phone.replace(/[\s()]/g, "")
res = @swissPhoneRegex.exec(phone)
if res then "0#{ res[3] } #{ res[4] } #{ res[5] } #{ res[6] }" else ""
@fphilipe
Copy link

Needed a break from studying. Here's a stricter and compacter regex:

Take a look an non-capturing groups as they don't pollute your matches: (?:a|b) instead of (a|b).

Here's a visualization of the regexp 😉

swissPhoneRegex = /^(?:(?:|0{1,2}|\+{0,2})41(?:|\(0\))|0)([1-9]\d)(\d{3})(\d{2})(\d{2})$/

validNumbers = """
0783268674
41783268674
041783268674
0041783268674
+41783268674
++41783268674
++41(0)783268674
""".split('\n')

invalidNumbers = """
783268674
00783268674
1783268674
+041783268674
00041783268674
+++41783268674
04178326867
(0)783268674
++410783268674
""".split('\n')

formatPhoneNumber = (phone) ->
  phone = phone.replace(/\s/g, '')
  matches = swissPhoneRegex.exec(phone)
  matches && "0#{matches[1]} #{matches[2]} #{matches[3]} #{matches[4]}"

test = ->
  for i in validNumbers
    return false if formatPhoneNumber(i) != '078 326 86 74'
  for i in invalidNumbers
    return false if formatPhoneNumber(i)
  true

@pozylon
Copy link

pozylon commented Feb 27, 2019

Da kollegä :D

^(0|0041|\+41)?[1-9\s][0-9\s]{1,12}$

@pozylon
Copy link

pozylon commented Feb 27, 2019

matched au mit leerstelle

@himynameisubik
Copy link

Da kollegä :D

^(0|0041|\+41)?[1-9\s][0-9\s]{1,12}$

Merci!

@munen
Copy link

munen commented Sep 20, 2020

^(0|0041|\+41)?[1-9\s][0-9\s]{1,12}$ feels too greedy. For example, it matches 05 .

Btw, hi @peyerluk 👋 Nice to find your content when searching the interwebs 🙏

@munen
Copy link

munen commented Sep 20, 2020

I have settled on this for the moment:

const internationalPhoneRegexp = /((\+|00)\d{8,30})/;

// Works for formatted mobile phone numbers like "078 326 86 74"
const swissPhoneRegexp1 = /(0[1-9]{2}\s[0-9]{3}\s[0-9]{2}\s[0-9]{2})/;

// Works for unformatted mobile phone numbers like "0783268674" and
// unformatted landline numbers like "041783268675"
const swissPhoneRegexp2 = /(0[0-9]{9,11})/;

const fullRegexp = new RegExp(
  [
    internationalPhoneRegexp.source,
    swissPhoneRegexp1.source,
    swissPhoneRegexp2.source,
  ].join('|'),
  'g'
);

It's not perfect, and will miss some formatting of phone numbers. However, it's less greedy, works for the formatted numbers that I currently care about and can be used without stripping whitespace.

It works for these test numbers:

  • 0783268674
  • 078 326 86 74
  • 041783268675
  • 0041783268674
  • +41783268676
  • +41783268677

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment