|
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", " "] |