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,624
  • 2
  • 11
  • 27
user3187960
  • 295
  • 2
  • 14
  • 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
  • 7,295
  • 6
  • 30
  • 53
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
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
  • 10,561
  • 2
  • 16
  • 29