0

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

2
  • Use the match method. Also your description is unclear since D and * are not numbers and are separated in the result. Commented Sep 16, 2017 at 6:12
  • What do you mean by "divide number" ? You want to extract the number from the input string ? You want to perform a division ? By the way, what is the meaning of "S", "D", "T" and "*" ? We need more context in order to provide accurate answers. Commented Sep 16, 2017 at 6:25

1 Answer 1

2

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

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

2 Comments

You should explain why you put the capture group in the regular expression. That's the important thing he was missing in his code.
capture group is for checking any number of digits in sequence.

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.