Javascript 根据数据动态屏蔽输入

Javascript 根据数据动态屏蔽输入,javascript,regex,maskedinput,Javascript,Regex,Maskedinput,我有一个响应,它返回“XXX-XXX”或“XX-XXXX” 所以基本上,我想看到的是formatUnitCode(“123456”,“XX-XXX”)-->“12-3456” 我不想使用if-else,因为它将来可能会以XX-XX-XX的形式出现 有人能帮我创建这个函数吗 我尝试使用正则表达式,但我认为不可能传递变量而不是{2}和{4} const formatCode = (val) => val.replace(/(\d{2})(\d{4})/g, "$1-$2")

我有一个响应,它返回“XXX-XXX”或“XX-XXXX”

所以基本上,我想看到的是
formatUnitCode(“123456”,“XX-XXX”)-->“12-3456”

我不想使用if-else,因为它将来可能会以XX-XX-XX的形式出现

有人能帮我创建这个函数吗

我尝试使用正则表达式,但我认为不可能传递变量而不是{2}和{4}

const formatCode = (val) => val.replace(/(\d{2})(\d{4})/g, "$1-$2");

不能在RegExp文本中使用变量,但可以在使用RegExp()构造函数将模式构建为字符串时使用变量

const formatStr = (val, format) => {
  let ptn = format.split('-').map(part => '(.{'+part.length+'})').join('');
      match = val.match(new RegExp(ptn));
  match && console.log(match.slice(1).join('-'));
};
查看
ptn
var的
console.log()
var来了解那里发生了什么是很有指导意义的。我们使用您任意的基于“X”的格式来派生一个新的动态RegExp,它将在多匹配RegExp中用于获取部件

formatStr('123456', 'xxx-xxx'); //"123-456"
formatStr('123456', 'xx-xxxx'); //"12-3456"

不能在RegExp文本中使用变量,但可以在使用RegExp()构造函数将模式构建为字符串时使用变量

const formatStr = (val, format) => {
  let ptn = format.split('-').map(part => '(.{'+part.length+'})').join('');
      match = val.match(new RegExp(ptn));
  match && console.log(match.slice(1).join('-'));
};
查看
ptn
var的
console.log()
var来了解那里发生了什么是很有指导意义的。我们使用您任意的基于“X”的格式来派生一个新的动态RegExp,它将在多匹配RegExp中用于获取部件

formatStr('123456', 'xxx-xxx'); //"123-456"
formatStr('123456', 'xx-xxxx'); //"12-3456"

可以使用simple for loop生成动态字符串格式方法

const formatUnitCode=(str,format)=>{
让结果=“”;
设j=0;
for(设i=0,l=format.length;iconsole.log(formatUnitCode('123456','XX-XX-XX')您可以使用simple for loop生成动态字符串格式方法

const formatUnitCode=(str,format)=>{
让结果=“”;
设j=0;
for(设i=0,l=format.length;iconsole.log(formatUnitCode('123456','XX-XX-XX')这是您想要做的吗

const func = (val, first_digit) => {
    let regex = new RegExp("(\\d{" + first_digit + "})(\\d{" + (6-first_digit) + "})","g");
    return val.replace(regex,"$1-$2");
};

这是你想做的吗

const func = (val, first_digit) => {
    let regex = new RegExp("(\\d{" + first_digit + "})(\\d{" + (6-first_digit) + "})","g");
    return val.replace(regex,"$1-$2");
};

这应该适用于任何掩码,而不考虑使用的字母(您可以通过更改matcher regex来控制该行为)。就我个人而言,我认为这是一种更具弹性的方法,而不仅仅是试图将给定的掩码与正则表达式匹配。
constreplacewithfiller=(filler,str,matcher=/[a-zA-z]/g)=>{
const arr=填充物拆分(“”);
返回str.replace(匹配器,()=>arr.shift());
};
console.log(replaceWithFiller('123456','XXX-XXX'))//"123-456"
console.log(replaceWithFiller('123456','XX-XX-XX'));//"12-34-56"
控制台日志(replaceWithFiller('123456','XX-XXXX')//"12-3456"

console.log(replaceWithFiller('123456','aa-aaaa');//另外,“12-3456”
这应该适用于任何掩码,无论使用的字母是什么(您可以通过更改matcher regex来控制该行为)。就我个人而言,我认为这是一种更具弹性的方法,而不仅仅是试图将给定的掩码与正则表达式匹配。
constreplacewithfiller=(filler,str,matcher=/[a-zA-z]/g)=>{
const arr=填充物拆分(“”);
返回str.replace(匹配器,()=>arr.shift());
};
console.log(replaceWithFiller('123456','XXX-XXX'))//"123-456"
console.log(replaceWithFiller('123456','XX-XX-XX'));//"12-34-56"
控制台日志(replaceWithFiller('123456','XX-XXXX')//"12-3456"

console.log(replaceWithFiller('123456','aa-aaaa');//另外,“12-3456”
您可以使用模板文本将参数传递给正则表达式:

const formatCode=(val,format)=>{
const lengthFirstBlock=format.indexOf('-');
const lehgthSecondBlock=format.length-format.indexOf('-');
const regex=new RegExp(`(\\d{${lengthFirstBlock}})(\\d{${lehgthSecondBlock}})`,'g');
返回值替换(regex,“$1-$2”);
} 
console.log(格式代码(“123456”,“XX-XXX”))

log(formatCode(“123456”,“XXX-XX”))
您可以使用模板文本将参数传递给正则表达式:

const formatCode=(val,format)=>{
const lengthFirstBlock=format.indexOf('-');
const lehgthSecondBlock=format.length-format.indexOf('-');
const regex=new RegExp(`(\\d{${lengthFirstBlock}})(\\d{${lehgthSecondBlock}})`,'g');
返回值替换(regex,“$1-$2”);
} 
console.log(格式代码(“123456”,“XX-XXX”))

console.log(formatCode(“123456”,“XXX-XX”))
礼貌的做法是向上投票或至少承认答案,特别是当他们解决了你的问题时。礼貌的做法是向上投票或至少承认答案,特别是当他们解决了你的问题时。