I have a string like this 100 and 20000
and I need to extract the numbers with regex and push them to array and access them by indices. Can you give me a little push here, my knowledge on regex are very limited. Thanks.
Asked
Active
Viewed 57 times
-2

worldwildwebdev
- 374
- 4
- 17
1 Answers
1
You can use \d+
to match numbers and match
to get the result in array.
Use g
global flag to match all the numbers.
var str = '100 and 20000';
var numbers = str.match(/\d+/g);
console.log(numbers); // numbers is an array: ["100", "20000"]
alert(numbers);

Tushar
- 85,780
- 21
- 159
- 179