kyun
kyun

Reputation: 10274

How to divide number and string from string array?

I have a question.

If I get "10S2D*3T" string, I have to divide number and not number like that.

What I want to do

INPUT

"10S2D*3T"

OUTPUT

[10, 'S', 2, 'D','*',3,'T']

It's my result

function solution(dartResult) {
    var answer = 0;
    var point = dartResult.split(/\D/gi);
    var option = dartResult.split(/\d/gi);
    console.log(point);
    console.log(option);
    return answer;
}

console.log(solution("10S2D*3T"));

Upvotes: 0

Views: 157

Answers (1)

anshu purohit
anshu purohit

Reputation: 176

You can use string split method and pass it a regular expression to check for digits and filter the array of empty strings.

var regExp = /(\d*)/;
var output = '10S2D*3T'.split(regExp).filter(Boolean);

output will be:
["10", "S", "2", "D", "*", "3", "T"]

https://i.sstatic.net/QAr3G.png

Upvotes: 2

Related Questions