Javascript Regex Question

I need a JavaScript Regex to do the following:

Example 1 - String = '0:9,1:5,2:6,3:1'  
Example 2 - String = '5:8'

I want to return everything after the ':' and before the ','.

Example 1 result:
9
5
6
1

Example 2 result:
8

Thanks
LostForWords1987Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rgonzo1971Commented:
Hi,

Pls try
const regex = /(.*?\:)(.*?)(?:,|$)/g;
const str = `0:9,1:5,2:6,3:1`;
const subst = `$2\r`;

// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);

console.log('Substitution result: ', result);

Open in new window

REgards
0
Pawan KumarDatabase ExpertCommented:
You have to use split function twice here to get the required values.
0
Julian HansenCommented:
If you want to see results one per line then borrowing from RGonzon's code and Pawan's suggestion

<script>
const str = '0:9,1:5,2:6,3:1';
var parts = str.split(',');
for(var i = 0; i< parts.length; i++) {
	console.log(parts[i].match(/(.*?\:)(.*)/)[2]);
}
</script>

Open in new window

Gives
9
5
6
1

Open in new window

And
<script>
const str = '5:8';
var parts = str.split(',');
for(var i = 0; i< parts.length; i++) {
	console.log(parts[i].match(/(.*?\:)(.*)/)[2]);
}
</script>

Open in new window

Gives
8

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LostForWords1987Author Commented:
Thanks guys. That's exactly what I was looking for.
0
Julian HansenCommented:
You are welcome.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.