替换字符后变量未定义

时间:2017-08-03 17:50:00

标签: jquery

我想替换在输入中输入的本地字符

var mapObj = {Č:"C", č:"c", Ć:"C", ć:"c", Đ:"D", đ:"d", Š:"S", š:"s", Ž:"Z", ž:"z"};

function clearlocale(x){
x = x.replace(/č|ć|đ|š|ž/i, function(matched) {
    return mapObj[matched];
});
}

$('#inpnew').keyup(function(e) {
    var a = $(this).val();
    console.log(a); //ok
    var b = clearlocale(a);
    console.log(b); // undefined
});

为什么b - undefined

1 个答案:

答案 0 :(得分:3)

  

为什么b - 未定义?

那是因为clearlocale没有返回任何内容。更换完成后返回x

function clearlocale(x){
  x = x.replace(/č|ć|đ|š|ž/i, function(matched) {
    return mapObj[matched];
  });
  return x;
}