Reputation: 35
I have a string tag:blogger.com,1999:blog-1234567.post-8912345 for example, and I want to get the last numbers "8912345", what is the best way to do this?
var str = "tag:blogger.com,1999:blog-1234567.post-8912345",
postId = str.match(/\d+$/)[0];
or
var str = "tag:blogger.com,1999:blog-1234567.post-8912345",
postId = str.split('-')[2];
Upvotes: 0
Views: 39
Reputation: 44087
Your first way was better, as it wouldn't matter how many dashes were before the number. You can also use destructuring to remove the [0]
:
var str = "tag:blogger.com,1999:blog-1234567.post-8912345",
[postId] = str.match(/\d+$/);
console.log(postId);
Upvotes: 1