您可以尝试如下的递归函数…
<script lang="javascript"> // Split string into all combinations possible function splitAllWays(result, left, right){ // Push current left + right to the result list result.push(left.concat(right)); //document.write(left.concat(right) + '<br />'); // If we still have chars to work with in the right side then keep splitting if (right.length > 1){ // For each combination left/right split call splitAllWays() for(var i = 1; i < right.length; i++){ splitAllWays(result, left.concat(right.substring(0, i)), right.substring(i)); } } // Return result return result; }; var str = "123456"; var ans = splitAllWays([], [], str);</script>结果
1234561,234561,2,34561,2,3,4561,2,3,4,561,2,3,4,5,61,2,3,45,61,2,34,561,2,34,5,61,2,345,61,23,4561,23,4,561,23,4,5,61,23,45,61,234,561,234,5,61,2345,612,345612,3,45612,3,4,5612,3,4,5,612,3,45,612,34,5612,34,5,612,345,6123,456123,4,56123,4,5,6123,45,61234,561234,5,612345,6
我认为这是正确的结果(32个组合)。有人可以确认吗?


![使所有可能的组合成一串数字,并在javascript中拆分?[关闭] 使所有可能的组合成一串数字,并在javascript中拆分?[关闭]](http://www.mshxw.com/aiimages/31/418405.png)
