Kevin Powell - CommunityKP-C
Kevin Powell - Community2y ago
12 replies
Matt

How to get a string within a string (JS)

Let's say for example, I have the following string in a URL

/?search_query=new%shoes&filter=shirts&color=red
or
/?filter=jackets&search_query=new%shoes&color=orange

or any other combination. What's the best approach at pulling only the search_query value from the string?
search_query=new%shoes would result in 'New Shoes'.

I need to do this dynamically to account for any size string and any order. I was thinking of using substring and trying to find the nearest index of say = and & or & and (nothing), if there is a case where URL is just /?search_query=new%shoes
Was this page helpful?