How to format this international phone number in Rails?

后端 未结 4 1899
情话喂你
情话喂你 2020-12-08 17:58

If I have an international phone number such as this:

0541754301

how can I format it to produce something like this:

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-08 18:30

    You can use regular expression to reformat the string. For the example you have given:

    "0541754301".sub(/(\d{4})(\d{3})(\d{3})/, "\\1-\\2-\\3") # returns: "0541-754-301"
    

提交回复
热议问题