Reputation: 585
I have url of the type:
localhost/Support/My-DNN-Module-Name/ID/133
I need to always extract the ID value at the end, in this case 133. This is what I am doing currently to extract it:
if (url) {
// try tograb number as int from url
var intValue = url.match( /\d+/ );
if (intValue) {
console.log("converted url to int value and the result is: " + intValue);
}
else {
console.log("could not convert url to int value");
}
}
Obviously this is not very good in case the URL contains other number(s), for example in the case that the localhost is entered as an actual ip address.
How can I target only the last part after ID and grab whatever number might be there please, ideally independent of what the url might look like before it?
Upvotes: 0
Views: 79
Reputation: 370679
Since the number you want always ends the string, you can add $
for the matched digits to be at the very end:
const str = 'localhost/12345/Support/My-DNN-Module-Name/ID/133';
const match = str.match(/\d+$/);
if (match) {
console.log(match[0]);
}
Upvotes: 1