Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use regexp to filter for question mark (?) parameter in url and add either ? or &

I have this:

var searchString = action +  ?  + 'search=' + search + '&filter='

the action variable could either already have a ?parameter or could not have one.

therefore I need to test action if it already contains a ? and if so append &search. Otherwise I need to use ?search

Any idea how to solve that in an easy manner?

like image 929
matt Avatar asked Feb 24 '23 06:02

matt


2 Answers

var searchString = action + (action.indexOf('?') != -1 ? '&' : '?') + 'search=' + search + '&filter=';
like image 181
Li0liQ Avatar answered May 01 '23 14:05

Li0liQ


var searchString = action + ((action.indexOf('?') == -1) ? '?search=' : '&search=');

Or you can use the jquery Query Plug-in http://plugins.jquery.com/project/query-object

like image 35
John Giotta Avatar answered May 01 '23 15:05

John Giotta