简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM