Skip to content

Instantly share code, notes, and snippets.

@ryanhanwu
Last active October 25, 2018 13:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanhanwu/9eafc4ae239a759a3a1b2bf303f97c9a to your computer and use it in GitHub Desktop.
Save ryanhanwu/9eafc4ae239a759a3a1b2bf303f97c9a to your computer and use it in GitHub Desktop.
Parse Double Quote #Search
module.exports= function parseKeywords(req, res, next){
const { q } = res.locals
//extract double quote terms
const re = /"(.*?)"/g
const phrases = []
let current
while ((current = re.exec(q))) {
phrases.push(current.pop())
}
//replace -_. with space for matches
let matchText = q.replace(/"(.*?)"/ig,'')
matchText = matchText.replace(/(-|\.|_)/ig,' ')
matchText = matchText.replace(/^\s+|\s+$|\s+/ig,' ').trim()
res.locals.query = {
matches: matchText.split(' '),
match_phrases: phrases,
}
next()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment