up vote 0 down vote favorite
share [g+] share [fb]

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?

thanks, Alon

link|improve this question

56% accept rate
feedback

1 Answer

Use a series 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 the split.

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

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.