Javascript 如何获得字符串中的所有单词组合

Javascript 如何获得字符串中的所有单词组合,javascript,Javascript,我想得到字符串形式的所有相邻单词组合 字符串获取所有组合 我想得到 get all combinations all combinations get all all get combinations 然后我编写下一个代码 var string='get all compositions'; var result=getKeywordsList(字符串); 记录(结果); 函数getKeywordsList(文本){ var wordList=text.split(“”); var关键字列表

我想得到字符串形式的所有相邻单词组合 字符串
获取所有组合
我想得到

get all combinations
all combinations
get all
all
get
combinations
然后我编写下一个代码

var string='get all compositions';
var result=getKeywordsList(字符串);
记录(结果);
函数getKeywordsList(文本){
var wordList=text.split(“”);
var关键字列表=[];
while(wordList.length>0){
keywordsList=keywordsList.concat(genKeyWords(wordList));
shift();
}
返回关键字列表;
}
函数genKeyWords(wordsList){
var res=[wordsList.join('');
如果(wordsList.length>1){
返回res.concat(类型关键字(wordsList.slice(0,-1));
}否则{
返回res;
}

}
您好,也许这对您有帮助

var string='get all compositions'
var sArray=string.split(“”);
var n=沙雷长度;
对于(变量i=0;ifor(var j=0;j)如果代码按预期工作,最好继续。@Sergey--小心使用递归函数,它们受到限制,取决于您使用的浏览器。事实上,只发送列表服务器端用PhP处理不是更好吗?@Grimbode谢谢,我会小心使用的it@Jongware我不知道这件事