I have a string
"B & D & P && D & P && B & C"
I'd like to split the string into a Javascript array by using the & or && as separators in order to get something like
"B, D, P, D, P, B, C"
I was wondering how I would approach this situation. Thanks!
You can use regex.
const str = "B & D & P && D & P && B & C";
console.log(str.split(/[\s&]+/g));
, is not needed, OP don't have a requirement to split by , also.You can do it easily by using regex. Try the following code
var str = 'B & D & P && D & P && B & C';
matches = str.match(/[^&]+/g);
console.log(matches);
/[^&\s*]+/g
&and&&separators? are these the only ones you will have?