1

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!

user1692517
  • 1,122
  • 4
  • 14
  • 28

2 Answers2

4

You can use regex.

const str = "B & D & P && D & P && B & C";

console.log(str.split(/[\s&]+/g));
Suren Srapyan
  • 66,568
  • 14
  • 114
  • 112
1

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);
PaulShovan
  • 2,140
  • 1
  • 13
  • 22