0

I have this string:

foo = "moon is.white #small"

I want to convert it to this array using rules such as after "." and "#" split and add & if there is no white space before it. I got the answer in this thread: How to Split string with multiple rules in javascript and this is what I got and it is great:

fooArr[0] = ['moon','is','&.white','#small']

My Question is - How do I make other variations of the string to get multiple-variation Array with these rules

A. split in different positions (sometimes the spaces will split and sometimes 2 words and more will be considers as one - in ALL variations (pay attention that if I don't split words with "." or "#" then I don't add "&".

fooArr[1] = ['moon is','&.white','#small']
fooArr[2] = ['moon is.white','#small']
foorArr[3] = ['moon','is.white','#small']
etc...

B. If there is "." or "#" then I want all variations of order between them -- ".all#is.good" can be --> [.all#is.good] & [.all.good#is] & [.good.all#is] etc... (and I want it combined with variations from the first rule such as [.all,&.good,&#is] & [.all.good,$#is]) so I will have ALL COMBINATIONS OF BOTH A AND B

Eventually I need an array combining all the combinations of A and B (it should give me a lot of variations: fooArr[0]..fooArr[X].

Where do I start?

Community
  • 1
  • 1
Alon
  • 7,618
  • 18
  • 61
  • 99

1 Answers1

0

Use a series of regular expression to find all the places where you want to split the string and replace it with " &". Then split on " ".

Or, use a loop to move through the string create replacements like above. And then split.

Or, use a loop to move through the string and build the array directly.

DG.
  • 3,417
  • 2
  • 23
  • 28