Skip to content

Instantly share code, notes, and snippets.

@rfprod
Last active April 22, 2017 15:43
Show Gist options
  • Save rfprod/56efc6f892cda473972967d5e5908080 to your computer and use it in GitHub Desktop.
Save rfprod/56efc6f892cda473972967d5e5908080 to your computer and use it in GitHub Desktop.
Split String Without Characters Loss
function splitWithoutCharLoss(str, split_p) {
//console.log('str:', str);
const splitter = split_p.split('|');
//console.log('splitter:', splitter);
let matches = [];
let regX;
if (splitter[0] && splitter[1]) { regX = new RegExp(splitter[0]+'(?='+splitter[1]+')'); }
else if (splitter[0] && !splitter[1]) { regX = new RegExp(splitter[0]); }
else if (!splitter[0] && splitter[1]) { regX = new RegExp(splitter[1]); }
// single split
matches.push(regX.exec(str));
// multiple splits
let strCopy = (splitter[0]) ? str.substring(matches[0].index + splitter[0].length, str.length) : str.substring(matches[0].index + splitter[1].length, str.length);
let nextIndex = regX.exec(strCopy);
while (nextIndex !== null) {
//console.log('strCopy:', strCopy);
matches.push(nextIndex);
strCopy = (splitter[0]) ? strCopy.substring(nextIndex.index + splitter[0].length, strCopy.length) : strCopy.substring(nextIndex.index + splitter[1].length, strCopy.length);
nextIndex = regX.exec(strCopy);
}
//console.log('regX | matches:', regX, '|', matches);
let split = [];
for (let i = 0, max = matches.length; i < max; i++) {
const match = matches[i];
//console.log('match:', match);
if (splitter[0] !== '') {
split.push(match.input.substring(0, match.index + match[0].length));
if (i === matches.length - 1) {
//console.log('end reached');
split.push(match.input.substring(match.index + match[0].length));
}
} else {
if (i !== matches.length - 1) {
const nextMatch = matches[i + 1];
if (i === 0 && match.index !== 0) {
split.push(match.input.substring(0, match.index));
}
split.push(match.input.substring(match.index, match.index + match[0].length + nextMatch.index));
} else {
//console.log('end reached');
if (i === 0 && match.index !== 0) {
split.push(match.input.substring(0, match.index));
}
split.push(match.input.substring(match.index));
}
}
}
return split.filter((item) => item.length > 0);
}
splitWithoutCharLoss("hello world! hello world!", "o|rl") // ["hello wo", "rld! hello wo", "rld!"]
splitWithoutCharLoss("hello world! hello world!", " |") // ["hello ", "world! ", "hello ", "world!"]
splitWithoutCharLoss(" hello world", " |") // [" ", "hello ", "world"]
splitWithoutCharLoss("hello hello hello ", "| ") // ["hello", " hello", " hello", " "]

Split String Without Characters Loss

Splits string similarly to String.split() but without delimiter loss. Uses | as a delimiter. Character sequence to the left of the delimiter specify the ending sequence of the splitted items. Character sequence to the right of the delimiter specify the starting sequence of the splitted items.

A script by V.

License.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment