Perhaps this approach would be best for this purpose:
var a = line.match(/([^:\s]+)\s*:\s*(.*)/); var key = a[1]; var val = a[2];
Thus, you can use tables in your configuration / data files of such a structure, and also not worry about spaces before or after the name separator ':' .
Or you can use the primitive and fast string functions indexOf and substr to achieve your goal, in my opinion, in the fastest way (CPU and RAM)
for ( ... line ... ) { var delimPos = line.indexOf(':'); if (delimPos <= 0) { continue; // Something wrong with this "line" } var key = line.substr(0, delimPos).trim(); var val = line.substr(delimPos + 1).trim(); // Do all you need with this key: val }
Flamestorm
source share