Ali
Ali

Reputation: 67

JS France phone number format

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

Upvotes: 1

Views: 868

Answers (1)

Mordy Stern
Mordy Stern

Reputation: 901

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, " ");

Upvotes: 1

Related Questions