gpt4 book ai didi

Javascript 对象不适用于 native javascript 方法,例如 match()、replace 等

转载 作者:行者123 更新时间:2023-11-29 17:13:58 25 4
gpt4 key购买 nike

问题是:

我去这个代码:

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));

看图:

enter image description here

我总是这样做:

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 an integer Value to the .match method which does not belong to integers. 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!

最佳答案

匹配函数适用于字符串。所以先把它转成字符串

str.Acc.toString().match(/[0-9]+/g)

关于Javascript 对象不适用于 native javascript 方法,例如 match()、replace 等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19701158/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com