0

I'm trying to come up with a regex that will do following. I have a string

var input_string = "E100T10P200E3000T3S10";

var output=input_string.split(**Trying to find this**);

this should give an array with all the letters in order with repetitions

output = ["E","T","P","E","T","S"]

4 Answers 4

1

See below. \d+ means one or more digits; filter (x => x) removes empty strings that can appear in the beginning or the end of the array if the input string begins or ends with digits.

var input_string = "E100T10P200E3000T3S10";

var output = input_string.split (/\d+/).filter (x => x);

console.log (output);

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

1 Comment

Thank you Alexander for very early and elegant reply, this solved my problem
0

We can try just matching for capital letters here:

var input_string = "E100T10P200E3000T3S10";
var output = input_string.match(/[A-Z]/g);
console.log(output);

Comments

0

Another approach is spread the string to array and use isNaN as filter callback

var input_string = "E100T10P200E3000T3S10";

var output = [...input_string].filter(isNaN);

console.log(output);

Comments

0

You can use regex replace method. First replace all the digits with empty string and then split the resultant string.

const input_string = 'E100T10P200E3000T3S10';
const ret = input_string.replace(/\d/g, '').split('');
console.log(ret);

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.