I wrote some regexp. It works when I execute it with string typed in console, but not in some cases in my script.
Here is my console output:
>body
["VERSION:2.1", "N:;S Wicius;;;", "FN:S Wicius", "TEL;PREF;CELL:000000000"]
>records.line
/^([^:;]+)(?:;([^:]+))?:(.+)$/gm
>records.line.exec( body[1] )
null
>body[1] == "N:;S Wicius;;;"
true
>records.line.exec( "N:;S Wicius;;;" )
["N:;S Wicius;;;", "N", undefined, ";S Wicius;;;"]
>for( var i = 0; i < body.length; i++ ) {
var line = [];
if( line = records.line.exec( body[i] ) )
console.log( line )
}
["VERSION:2.1", "VERSION", undefined, "2.1"]
["FN:S Wicius", "FN", undefined, "S Wicius"]
Well, the problem is caused by the combination of the following factors:
execmethod changes lastIndex property of your regexp objectYou can fix the issue by changing any of these 3 conditions (you also could manually reset
records.line.lastIndex = 0in each iteration). Your /g modifier seems to be useless, so just get rid of it.