Claudius Ibn Claudius Ibn - 1 month ago 12
Javascript Question

Extract id from string with javascript

Facebook api returns the following messages:


"Page ID 1516333402003734 was migrated to page ID 967771353292822.
Please update your API calls to the new ID"


Extraction:

oldId = 1516333402003734
newId = 967771353292822



"(#803) Some of the aliases you requested do not exist:
40270085647410"


Extraction:

id = 40270085647410


I need to extract the ids from strings and assign to variables, like the extractions examples. What's the best way to achieve this?

Answer

This should do it:

var s = "Page ID 1516333402003734 was migrated to page ID 967771353292822. Please update your API calls to the new ID";
var ids = s.match(/(\d+)/g);
var oldId = ids[0];
var newId = ids[1];
console.log("Old ID:", oldId);
console.log("New ID:", newId);
s = "(#803) Some of the aliases you requested do not exist: 40270085647410"
id = s.match(/\d+$/)[0];
console.log("ID:", id)

Comments