Last active
June 6, 2017 09:45
-
-
Save tyv/7ab6152956518a4f0d11c3bbdb219b39 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'pp'.split('p') // ["", "", ""] | |
'apple'.split('p') // ["a", "", "le"] | |
// need 'apple' as ["a", "", "", "le"] | |
// 'apppple' with split by 'pp' should be ["a", "", "", "le"] as well |
const splitter = (word, splitPhrase) => {
let replaceCount = 0;
const res = word
.match(new RegExp(`(${splitPhrase})`))
.reduce(() => {
replaceCount++;
return word.replace(`(${splitPhrase})`, splitPhrase + splitPhrase);
}, '')
.split(new RegExp(`(${splitPhrase})`))
.filter(l => l.length)
.map(l => l === splitPhrase ? '' : l);
return res.length === replaceCount ? res.concat('') : res;
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
¯_(ツ)_/¯