Last active
October 25, 2018 13:50
-
-
Save ryanhanwu/9eafc4ae239a759a3a1b2bf303f97c9a to your computer and use it in GitHub Desktop.
Parse Double Quote #Search
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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