Skip to content

Instantly share code, notes, and snippets.

@larryyangsen
Last active October 2, 2017 03:44
Show Gist options
  • Select an option

  • Save larryyangsen/e108291dc6ef010bd080445f20b0782d to your computer and use it in GitHub Desktop.

Select an option

Save larryyangsen/e108291dc6ef010bd080445f20b0782d to your computer and use it in GitHub Desktop.
recursive split string with a sign
const split = (sign = '/') => (str = '') => {
if (!str.includes(sign)) return str.trim();
const arr = str.split(' ');
const signIndex = arr.findIndex(a => a.includes(sign));
const scrapeStr = arr.slice(0, signIndex).join(' ');
const splits = arr[signIndex].split(sign);
const sliced = arr.slice(signIndex + 1).join(' ');
const strs = [];
for (const s of splits) {
s && strs.push(split(sign)(`${scrapeStr} ${s} ${sliced}`));
}
return strs.join('\n');
};
const splitScrap = split('/');
console.log(splitScrap('Today/Tomorrow/Yesterday Hello/Hi there nice to meet/see you'));
const a = splitScrap('1/2/3/7 8/9 4');
console.log(a);
// Today Hello there nice to meet you
// Today Hello there nice to see you
// Today Hi there nice to meet you
// Today Hi there nice to see you
// Tomorrow Hello there nice to meet you
// Tomorrow Hello there nice to see you
// Tomorrow Hi there nice to meet you
// Tomorrow Hi there nice to see you
// Yesterday Hello there nice to meet you
// Yesterday Hello there nice to see you
// Yesterday Hi there nice to meet you
// Yesterday Hi there nice to see you
// 1 8 4
// 1 9 4
// 2 8 4
// 2 9 4
// 3 8 4
// 3 9 4
// 7 8 4
// 7 9 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment