如何生成一个子字符串数组,这些子字符串是一串单词的某些单词组合?
How does one generate an array of sub-strings which are certain word combinations of a string of words?
输入:美国
输出:[United, States, America, States America, United States, United States America]
function splitAndRecombine(value) {
return String(value)
.split(/\s+/)
.reduce((result, item, idx, arr) => {
const rest = arr.slice(idx);
result.push(rest.join(' '));
while (rest.pop() && rest.length >= 1) {
result.push(rest.join(' '));
}
return result;
}, []);
}
console.log(
"splitAndRecombine('United States America') ...",
splitAndRecombine('United States America')
);
console.log(
"splitAndRecombine('United States of America') ...",
splitAndRecombine('United States of America')
);
console.log(
"splitAndRecombine('United Kingdom of Great Britain and Northern Ireland') ...",
splitAndRecombine('United Kingdom of Great Britain and Northern Ireland')
);
.as-console-wrapper { min-height: 100%!important; top: 0; }
输入:美国
输出:[United, States, America, States America, United States, United States America]
function splitAndRecombine(value) {
return String(value)
.split(/\s+/)
.reduce((result, item, idx, arr) => {
const rest = arr.slice(idx);
result.push(rest.join(' '));
while (rest.pop() && rest.length >= 1) {
result.push(rest.join(' '));
}
return result;
}, []);
}
console.log(
"splitAndRecombine('United States America') ...",
splitAndRecombine('United States America')
);
console.log(
"splitAndRecombine('United States of America') ...",
splitAndRecombine('United States of America')
);
console.log(
"splitAndRecombine('United Kingdom of Great Britain and Northern Ireland') ...",
splitAndRecombine('United Kingdom of Great Britain and Northern Ireland')
);
.as-console-wrapper { min-height: 100%!important; top: 0; }