1
'https://twitter.com/pixelhenk/status/891303699204714496'.match(/\/status\/(\d+)/g)

gives me

[ "/status/891303699204714496" ]

How do I get just the number? I thought putting it in parentheses did this, but apparently not.

4
  • Use exec() instead. Commented Jan 11, 2018 at 15:46
  • The number is in the first capturing group. Commented Jan 11, 2018 at 15:46
  • @ctwheels: We can mark multiple ones. :-) Commented Jan 11, 2018 at 15:48
  • Does it HAVE to be regex? const num = 'https://twitter.com/pixelhenk/status/891303699204714496'.split("/").pop(); Commented Jan 11, 2018 at 15:50

1 Answer 1

5

Yes, a capture group does this. You don't see it in your array because you've used String#match and the g flag. Either remove the g flag (and take the second entry from the array, which is the first capture group):

console.log('https://twitter.com/pixelhenk/status/891303699204714496'.match(/\/status\/(\d+)/)[1]);

...or use RegExp#exec instead of String#match.

console.log(/\/status\/(\d+)/g.exec('https://twitter.com/pixelhenk/status/891303699204714496')[1]);

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.