开发者问题收集

调用 replace 方法时,TypeError:undefined 不是一个函数

2014-08-16
1407

我知道代码很少,我遗漏了一些小东西。

小提琴: http://jsfiddle.net/0oa9006e/1/

代码:

var veri = "+???+Girdiğiniz eposta adresi 'adfadf' geçersiz.-???-";

var a = veri.match(/\+[\?]*\+(.*)*\-[\?]*\-/g);
 a = a.replace(/[\+\-\?]*/g , "");
alert(a);
3个回答

String.match(param) 方法返回一个包含所有匹配项的数组。而 javascript 中的数组没有 .replace 方法 。因此出现错误。您可以尝试类似以下方法:

 a = a.toString().replace(/[\+\-\?]*/g,""); // Array to string converstion
Vivek Pratap Singh
2014-08-16

您的匹配返回一个数组,其中没有 replace 。尝试:

a = a[0].replace(/[\+\-\?]*/g , "");
vch
2014-08-16
var veri = "+???+Girdiğiniz eposta adresi 'adfadf' geçersiz.-???-";


var a = veri.match(/\+[\?]*\+(.*)*\-[\?]*\-/g);
// The variable 'a' is now an array.
// The first step in debugging is to always make sure you have the values
// you think you have.
console.log(a);

// Arrays have no replace method.
// Perhaps you are trying to access a[0]?
// or did you mean to modify `veri`?
 a = a.replace(/[\+\-\?]*/g , "");
alert(a);
Jeremy J Starcher
2014-08-16