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?

2

2 Answers 2

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);

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

Comments

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.