I am using lunr.js.
This Javascript code below
- gets the query parameters from the first search
- replaces the binding + characters with spaces
- stores the modified query in the search field for the next search
- Then it finally searches using the modified query
var query = getUrlParameter('q');
var queryWithoutPlus = query.replace(/\+/g, " ");
searchField.value = queryWithoutPlus
index.search(queryWithoutPlus);
So, a query param string could look like test+ABCD+Test++Test2+-Test+-Test+Test
and the code replaces the + characters in the query parameter q
with spaces in order to show the result to the user in a nice manner, but a search string like
"test +test -test"
currently results in
"test test -test"
I would expect
"test +test -test"
I tried to modify the query several times in a row using a tempQuery like
var tempQuery = query.replace(/\+\+/g, " -");
var queryWithoutPlus = tempQuery.replace(/\+\-/g, " -");
but this doesn't work out with the remaining + characters and does not feel right, so I need your help.
Does it just boil down to use the correct regex (whatever it might be, advice welcome) or is there is even a better approach for using query parameters with lunr.js?
You need a negative lookbehind query (?<!\+)
"test+ABCD+Test++Test2+-Test+-Test+Test".replace(/(?<!\+)\+/g, " ")
>> "test ABCD Test +Test2 -Test -Test Test"