开发者

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 &sea开发者_开发知识库rch. Otherwise I need to use ?search

Any idea how to solve that in an easy manner?


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


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

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


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


quick, not super readable way:

var hasQM = (/\?/).test(action);
var searchString = action + (!hasQM ? '?' : '&') + 'search=' + search + '&filter='


var string = "?somerandonmestuffhere";
alert((string.match(/[?].*/) || ["?" + string])[0])
var string = "somerandonmestuffhere";
alert((string.match(/[?].*/) || ["?" + string])[0])
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜