如何在第七个字符中放入" " - 空格?


/* Máscaras ER */
function mascara(o,f){
v_obj=o
v_fun=f
setTimeout("execmascara()",1)
}
function execmascara(){
v_obj.value=v_fun(v_obj.value)
}
function mtel(v){

v=v.replace(/D/g,"");             //Remove tudo o que não é dígito
v=v.replace(/^(d{2})(d)/g,"+$1 $2"); //Coloca parênteses em volta dos dois primeiros dígitos
v=v.replace(/(d)(d{4})$/,"$1-$2");    //Coloca hífen entre o quarto e o quinto dígitos
return v;
}
function id( el ){
return document.getElementById( el );
}
window.onload = function(){
id('form-field-telefone').setAttribute('maxlength',16);
id('form-field-telefone').onkeypress =
function(){
mascara( this, mtel );
}
}

我在输入中有以下结果:→+ 11 1111111 - 1111

我想在这里放一个空格:→+11 11 11111-1111

可以使用

"+11 1111111-1111".replace(/(.{6})(.+)/, "$1 $2")

您可以使用concat和substring

创建一个新字符串
var input = '+11 1111111-1111'
var output = input.substring(0, 6).concat(' ').concat(input.substring(6, input.length))

最新更新