Home > Software engineering >  JS France phone number format
JS France phone number format

Time:11-28

I want to format French phone number on input. For example if inputed

0XXXXXXXXX need transform to 0X XX XX XX XX , which one I already achieved with replace:

.replace(/\B(?=(\d{2}) (?!\d))/g, " ");

But I also want to format if number starts with 33

33XXXXXXXXX need transform to 33 X XX XX XX XX

How can I achieve that? note: I need it in same input

Thanks

CodePudding user response:

firstly, add to your expression a negative lookbehind for 3 (?<!\ 3) so it should not add a space after the 3

secondly, add another option with a logical OR operator | with a positive lookbehind for 33 \B(?<=\ 33)

so the final result will be:

.replace(/\B(?=(\d{2}) (?!\d))(?<!\ 3)|\B(?<=\ 33)/g, " ");
  • Related