0

I am trying to get an array of numbers from a string that does not have a token to use as a split.

Example:

var myString = 'someString5oneMoreString6';

Expected result :

var result = [5, 6];

How to archive this with javascript before ES2015?

Ashish Patil
  • 1,628
  • 2
  • 9
  • 25
user3187960
  • 277
  • 2
  • 13
  • Does this answer your question? [Remove all special characters with RegExp](https://stackoverflow.com/questions/4374822/remove-all-special-characters-with-regexp) – Erazihel Apr 14 '20 at 09:49
  • Does this answer your question? [Extract ("get") a number from a string](https://stackoverflow.com/questions/10003683/extract-get-a-number-from-a-string) – Pol Vilarrasa Apr 14 '20 at 10:12

2 Answers2

1

You could match all digits and convert the result to numbers.

var string = 'someString5oneMoreString6',
    array = string.match(/\d+/g);

for (var i = 0; i < array.length; i++) array[i] = +array[i];

console.log(array);
Erazihel
  • 6,815
  • 5
  • 28
  • 51
Nina Scholz
  • 351,820
  • 24
  • 303
  • 358
0

you can split by regex and map to process.

var str = 'someString5oneMoreString6';

const array = str.split(/[a-zA-Z]+/).filter(x => x).map(x => Number(x));

console.log(array);
Siva K V
  • 9,737
  • 2
  • 13
  • 29