1
0

Do not split on spaces inside simple and double quotes

Use a regex to identify search terms more reliably than spliting on a space.
Does NOT respect escaping simple or double quotes within a term since JS
does not support look-behinds in regexs.
This commit is contained in:
Daniel Spofford
2015-05-02 21:41:25 -05:00
parent 5215b3dfb0
commit a631329ea5

View File

@@ -27,8 +27,14 @@ exports.parse = function (string, options) {
else {
// Our object to store the query object
var query = {text: []};
// Get a list of search term. Reverse to ensure proper order when pop()'ing.
var terms = string.split(' ').reverse();
// Get a list of search terms respecting single and double quotes
var terms = string.match(/(\S+:(".+?"|'.+?'))|(\S+:\S+)|\S+/g);
// Rip out the quotes
for (var i = 0; i < terms.length; i++) {
terms[i] = terms[i].replace(/['"]+/g, '');
};
// Reverse to ensure proper order when pop()'ing.
terms.reverse();
// For each search term
while (term = terms.pop()) {
// Advanced search terms syntax has key and value