问题是这样的:
我去这个代码:
var str = {"Acc":10 , "adm_data":"Denied"};
当我做类似的事情时:
console.log(str.Acc.match(/[0-9]+/g)) // To Get the Integer Value from the "Acc" key
Firebug 尖叫声:
TypeError: str.Acc.match is not a function
console.log(str.Acc.match(/[0-9]+/g));
看图片:
我总是做这样的事情:
var str = "Hello _10";
console.log(str.match(/[0-9]+/g)) // This Works
为什么Object
东西不工作?
PLEASE NOTE:
As mentioned by
@Fabrício Matté.
The issue was that I was trying to pass aninteger
Value to the.match
method which does not belong tointegers
. The solution was to do what@kundan Karn
Suggested. Something like:str.Acc.toString().match(/[0-9]+/g)// Converting it first to string then match
. It worked!
最佳答案
match 函数适用于字符串。所以先将其转换为字符串
str.Acc.toString().match(/[0-9]+/g)
关于Javascript 对象无法与 native javascript 方法(如 match()、替换等)一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19701158/