对于微小但非常令人沮丧的问题,我们将不胜感激。我正在使用 element plus 库开发 Vue 项目。 用户输入:'123456789' 我需要拨打:'+998-(12) 345-67-89
Element plus 有格式化程序,但我需要使用正则表达式来制作它。不幸的是我很难让它发挥作用。 https://element-plus.org/en-US/component/input.html#formatter
我现在只检查了号码,无法进一步了解
<script setup>
import { ref, unref } from 'vue'
import { ElInput } from 'element-plus'
const phoneNumber = ref('')
</script>
<template>
  <el-input v-model="phoneNumber" :formatter="(value) => value.replace(/D/g, '')" />
</template>
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
您可以使用以下正则表达式:
value.replace(/^\+998|\D/g, '').replace(/^(\d{1,2})(\d{1,3})?(\d{1,2})?(\d{1,2})?.*/, (m, g1, g2, g3, g4) => `+998-(${g1})` + (g2 ? `-${g2}` : '') + (g3 ? `-${g3}` : '') + (g4 ? `-${g4}` : '')))第一个
.replace(/^\+998|\D/g, '')删除字符串开头的+998(由成功的后续替换)和任何非数字字符,以及replace(/^(\d{1,2})(\d{1,3})?(\d{1,2})?(\ d{1,2})?.*/, (m, g1, g2, g3, g4) => `+998-(${g1})` + (g2 ? `-${g2}` : '' ) + (g3 ? `-${g3}` : '') + (g4 ? `-${g4}` : ''))通过添加-重新格式化数字> 仅在必要时在您键入时启动。