调用replace方法时,TypeError: undefined不是函数
TypeError : undefined is not a function when calling replace method
我知道代码很少,我错过了一些小的东西。
小提琴:http://jsfiddle.net/0oa9006e/1/
code:
var veri = "+???+Girdiğiniz eposta adresi 'adfadf' geçersiz.-???-";
var a = veri.match(/'+['?]*'+(.*)*'-['?]*'-/g);
a = a.replace(/['+'-'?]*/g , "");
alert(a);
String.match(param)
方法返回一个包含所有匹配项的数组。和数组在javascript中没有.replace method
。因此错误。您可以尝试这样做:
a = a.toString().replace(/['+'-'?]*/g,""); // Array to string converstion
您的匹配返回一个数组,它没有replace
。试一试:
a = a[0].replace(/['+'-'?]*/g , "");
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);
当veri.match(/'+['?]*'+(.*)*'-['?]*'-/g)
被执行时,你的变量a
被初始化为一个JavaScript数组,它没有replace
方法。
使用Regex101之类的RegEx工具查看正则表达式如何匹配字符串veri
,然后对该数组的适当元素执行replace
操作。
下面是使用正则表达式的示例:http://regex101.com/r/hG3uI1/1
如您所见,您的正则表达式匹配veri
保存的整个字符串,因此您希望对match
返回的第一个(也是唯一一个)元素执行replace
操作:
a = a[0].replace(/['+'-'?]*/g , "");
相关文章:
- 为什么会出现错误;未捕获的类型错误:undefined不是函数;
- 为什么会出现错误,“UncaughtTypeError:undefined不是函数”
- mongoose.connect undefined不是一个函数
- Uncaught TypeError:undefined不是函数-ember js
- jQuery Mobile Undefined不是一个函数
- 未捕获的类型错误:undefined不是函数$
- 使用在函数外部声明的变量的 Javascript 返回值 + undefined
- 为什么Grunt Copy给了我一个'undefined不是函数'警告
- Bootstrap-“;未捕获的类型错误:undefined不是函数;
- Undefined不是函数ImpressPages DatePicker
- 尝试使用.ninnerHTMLlog打印数据,获取错误Uncaught TypeError:undefined不是函数
- 得到“;undefined不是函数“;错误
- Undefined不是Fullcalendar js上的函数
- 使用谷歌地图:一个返回undefined的自定义javascript函数
- jqGrid:undefined不是函数
- Jquery,Uncaught TypeError:undefined不是函数
- $http.post undefined 不是函数
- Ember cli Uncatch TypeError: undefined 不是函数
- Infragistics igGrid Uncatch TypeError: undefined 不是一个函数
- JQuery脚本在Firefox上工作;undefined不是函数“;铬