How to extract array of numbers from string in javascript
arrays, javascript, regex
Solution
var numbers = '[stuff ids="7,80"]'.match(/\d+/g);
`\d+` matches any consecutive digits (which is number) and `g` modifier tells to match all
PS: in case if you need to match negative numbers:
var numbers = '[stuff ids="-7,80"]'.match(/-?\d+/g);
Problem
Using javascript, I need to extract the numbers from this string: ``` [stuff ids="7,80"] ``` and the string can have anywhere from one to five sets of numbers, separated by commas (with each set having 1 or more digits) that need to be extracted into an array. I've tried: ``` var input = '[stuff ids="7,80"]'; var matches = input.match(/ids="(\d*),(\d*)"/); ``` This will give me an array with 7 and 80 in it (I think), but how do I take this further so that it will return all of the numbers if there are more than two (or less than two)? Further, is this even the best way to go about this? Thanks for the help!