简体   繁体   中英

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

I have a string "{\\"id\\":\\"35112914\\"}" obtained after JSON.stringify . I want just the number 35112914 or the first instance of a number ,ie 3 to the last ,ie, 4 . Is there any other way of approaching this ?

Edit: I had to stringify because of a weird error I was getting. While accessing the property of the object, i was getting undefined as the answer , which made the number inaccessible. Stringify atleast shows up the number .

Edit 2 :

My code is written in node which makes curl request to a REST api .

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

Output:

{"id":"35113281"}
undefined

You need to parse your response to a JS object before you can access it using JS object methods:

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

DEMO

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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