Reputation: 298
I'm not a regex-master, but I'm looking for a regex that would give this result in js:
var regex = ...;
var result = '"a b", "c, d", e f, g, "h"'.match(regex);
and result would be
['"a b"', '"c, d"', 'e f', 'g', '"h"']
EDIT:
Escaped quotes don't need to be handled. It's for a tagging field, where users must be able to enter:
tag1, tag2
but also
"New York, USA", "Boston, USA"
EDIT2: Thank you for your amazingly quick answer minitech, that did the trick!
Upvotes: 0
Views: 189
Reputation: 43703
var result = input.match(/(?:(?:"((?:[^"]|"")*)")|([^",\n]*))/g);
for (var i = 0; i < result.length; i++) {
result[i] = result[i].replace(/^\s*/, "").replace(/\s*$/, "");
if (result[i].length === 0) {
result.splice(i--, 1);
}
}
Test this code here.
Upvotes: 0
Reputation: 129139
Regular expressions may not be the best tool for this task. You may want to instead do it instead by looping through the characters and deciding what to do. Here's some pseudocode that would do that:
Upvotes: 1
Reputation: 225269
I'd just use a loop:
function splitCSVFields(row) {
var result = [];
var i, c, q = false;
var current = '';
for(i = 0; c = row.charAt(i); i++) {
if(c === '"') {
current += c;
q = !q;
} else if(c === ',' && !q) {
result.push(current.trim());
current = '';
} else {
current += c;
}
}
if(row.length > 0) {
result.push(current.trim());
}
return result;
}
Note: requires String#trim
, which you can shiv as follows:
if(!String.prototype.trim) {
String.prototype.trim = function() {
return this.replace(/^\s+/, '').replace(/\s+$/, '');
};
}
Upvotes: 3