Alex Alex - 5 months ago 10
jQuery Question

jQuery/JS extract a specific string from URL

I have the following URL:

http://www.xxxxxxxxx.com/search?search_keywords=Rome%2C+Italy&country=Italy&city=Rome&date_from=2016-07-02&date_to=2016-07-05

I only need to extract Rome from the url. So far I have managed to do this:

var citySearch = $.cookie('recentSearch');
citySearch.match(/_keywords=(.+)/)[1]


where citySearch the url.

This only returns "Rome%2C+Italy&country=Italy&city=Rome&date_from=2016-07-02&date_to=2016-07-05
"

Answer

your regex is matching too much, it should work with:

var citySearch = $.cookie('recentSearch');
citySearch.match(/_keywords=(.+?)&/)[1]

This would give you "Rome%2C+Italy" which is the keyword parameter.

hope this helps