1
0

Respect escaped characters while also respecting quoted spaces

This commit is contained in:
Daniel Spofford
2015-05-03 00:11:00 -05:00
parent a631329ea5
commit 5090897e18

View File

@@ -28,10 +28,29 @@ exports.parse = function (string, options) {
// Our object to store the query object
var query = {text: []};
// Get a list of search terms respecting single and double quotes
var terms = string.match(/(\S+:(".+?"|'.+?'))|(\S+:\S+)|\S+/g);
// Rip out the quotes
var terms = string.match(/(\S+:'(?:[^'\\]|\\.)*')|(\S+:"(?:[^"\\]|\\.)*")|\S+|\S+:\S+/g);
for (var i = 0; i < terms.length; i++) {
terms[i] = terms[i].replace(/['"]+/g, '');
if(terms[i].indexOf(':') !== -1) {
var split = terms[i].split(':'),
key = split[0],
val = split[1];
// Strip surrounding quotes
val = val.replace(/^\"|\"$|^\'|\'$/g, '');
// Strip backslashes respecting escapes
val = (val + '').replace(/\\(.?)/g, function (s, n1) {
switch (n1) {
case '\\':
return '\\';
case '0':
return '\u0000';
case '':
return '';
default:
return n1;
}
});
terms[i] = key + ':' + val;
}
};
// Reverse to ensure proper order when pop()'ing.
terms.reverse();