I have a string "Color of model: White/White/White". I want to find out, how to get all colors(White,White,White) without 'slashes' and make array from this values.Quantity of colors can change (White/Green/Yellow/Black etc)
Asked
Active
Viewed 45 times
-4
-
You'll get a better answer if you tag this with which language you are interested in. – Maess May 23 '18 at 15:06
-
@maess i don't get your point. – Jonas Wilms May 23 '18 at 15:07
2 Answers
4
You need to use split:
let colors = 'Red/White/Blue'
console.log(colors.split('/'))
If you need to include Color of model:
you will need to split twice, once on the :
and then on the second array parameter.
let colors = 'Color of model: Red/White/Blue/Green/Purple/Black'
console.log(colors.split(/:\s+/, 2)[1].split('/'))

Get Off My Lawn
- 34,175
- 38
- 176
- 338
-
1Based on his example: "Color of model: White/White/White" you need an additional first split on ':' – Maess May 23 '18 at 15:07
-
I wasn't sure that `Color of model: ` was part of the string... I'll fix – Get Off My Lawn May 23 '18 at 15:08
2
May be you can do the following by using String.prototype.split()
:
var str = "Color of model: White/White/White";
var color = str.split(':')[1].split('/').join(',');
console.log(color);

Mamun
- 66,969
- 9
- 47
- 59