Skip to content

Instantly share code, notes, and snippets.

@afunTW
Created November 18, 2016 08:28
Show Gist options
  • Save afunTW/86b84dc2cf4d46407fc2c46db23dd786 to your computer and use it in GitHub Desktop.
Save afunTW/86b84dc2cf4d46407fc2c46db23dd786 to your computer and use it in GitHub Desktop.
Simple parser for AND and OR to javascript regular script
/*
* input = "apple AND orange"; re = "(?=apple)(?=orange)"
* input = "apple OR orange"; re = "apple|orange"
*/
function queryParser (input) {
var re = ''
input = input.split(' ')
for (var i=0; i<input.length; ++i) {
if (input[i] !== 'AND' &&
input[i] !== 'OR' &&
input[i] !== '') {
input[i] = '(?=.*'+input[i]+')'
}
input[i] = (input[i] == 'AND') ? '': input[i]
input[i] = (input[i] == 'OR') ? '|': input[i]
re += input[i]
}
return re
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment