简体   繁体   English

JS法国电话号码格式

[英]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: 0XXXXXXXXX 需要转换为 0X XX XX XX XX,我已经用 replace 实现了:

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

But I also want to format if number starts with +33但如果数字以 +33 开头,我也想格式化

+33XXXXXXXXX need transform to +33 X XX XX XX XX +33XXXXXXXXX 需要转换为 +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首先,在表达式中添加 +3 (?<!\+3)的负后视,因此不应在 +3 后添加空格

secondly, add another option with a logical OR operator |其次,使用逻辑或运算符|添加另一个选项with a positive lookbehind for +33 \B(?<=\+33)对 +33 \B(?<=\+33)进行正面回顾

so the final result will be:所以最终结果将是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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