I have a regex which will split my string into arrays, if it finds NULL\n or '\n.
My string is:
"'<xml↵ data>', NULL↵'abc', '<xml↵ data>'↵'abc', 'abc'"
(String has values separated with comma, all values are wrapped with single quote, except NULL values. New rows are defined with new lines, but my real problem is that values can also have new lines.)
With /NULL\n|'\n/
I get this result:
["'<xml↵ data>', ", "'abc', '<xml↵ data>", "'abc', 'abc'"]
But now I would like to keep NULL and ' part of the delimiter (I'm also ok if \n is preserved). So it would look like this:
["'<xml↵ data>', NULL", "'abc', '<xml↵ data>'", "'abc', 'abc'"]
My code so far:
var data = "'<xml\n data>', NULL\n'abc', '<xml\n data>'\n'abc', 'abc'"
var result = data.split(/NULL\n|'\n/)
console.log(result)
Thank you very much for your help. I now similar threads exist (like this one) but i'm not good in regex so i was not successful when transforming solutions for my needs.
EDIT: Working solution (if anybody else needs it)
From @Michael Sanchez answer I created this working function based on indexOf (although I'm a little worried from the performance point of view, because in my case the loop must go over 4MB large text):
Live demo: http://jsfiddle.net/ngr97jz7/3/
function ConvertToArray(text){
var rows = [];
var i = 1;
while(i != -1 && i != 0){
//find closer appearance
var a = text.indexOf("NULL\n");
var b = text.indexOf("'\n");
i = ((a < b && a != -1) || (a > b && b == -1)) ? a+4 : b+1; //set index + 4 chars for NULL or 1 char for '
if(i == 0 || i == -1){
rows.push( text );
break;
}
rows.push( text.substring(0,i) );
text = text.substring(i+1, text.length)
}
return rows;
}