繁体   English   中英

如何在JavaScript正则表达式/对象错误中获取字符串的数字的第一个实例到最后一个实例?

[英]How to get first instance to last instance of a number in a string in javascript regex/ object error?

我在JSON.stringify之后获得了字符串"{\\"id\\":\\"35112914\\"}" 我只想要数字35112914或数字的第一个实例,即3到最后一个即4 还有其他解决方法吗?

编辑:由于我得到一个奇怪的错误,我不得不进行分类。 在访问对象的属性时,我undefined得到答案的答案,这使得该数字不可访问。 Stringify atleast显示数字。

编辑2:

我的代码写在node上,它向REST api发出curl请求。

curl.request(optionsRun, function (error, response) {
                console.log(response);
                setTimeout(function () {
                    ID = response ;
                    console.log(ID["id"]) ;
                },2000) ;
            }) ;

输出:

{"id":"35113281"}
undefined

您需要先解析对JS对象的响应,然后才能使用JS对象方法访问它:

curl.request(optionsRun, function (error, response) {
  var ID = JSON.parse(response);
  setTimeout(function () {
    console.log(ID.id) ;
  }, 2000);
});

演示

此正则表达式将为您提供两个引号/\\"(\\d+)\\"/之间的任何数字组合

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM