0

I have a URL

https://www.yellowpages.com/search?search_terms=Generator+Repair&geo_location_terms=Adamsville%2C+Alabama

I want to get search_terms (Generator+Repair) and geo_location_terms (Adamsville%2C+Alabama)

How I can do this?

Matt Morgan
5,3334 gold badges24 silver badges34 bronze badges
asked Nov 12, 2018 at 17:42
1
  • You can use regex to do that. Or use a URL parser library.Don't have name but you can search Commented Dec 8, 2018 at 0:16

3 Answers 3

1

The easiest and most idiomatic way to do this in JavaScript is using the URL class:

const url = new URL('https://www.yellowpages.com/search?search_terms=Generator+Repair&geo_location_terms=Adamsville%2C+Alabama')
console.log(url.searchParams.get('search_terms'));
console.log(url.searchParams.get('geo_location_terms'));

MDN reference here.

answered Nov 12, 2018 at 18:24
Sign up to request clarification or add additional context in comments.

Comments

0

You can use the following Javascript code to store the GET parameters into an object:

<script>
 var URL = "https://www.yellowpages.com/search?search_terms=Generator+Repair&geo_location_terms=Adamsville%2C+Alabama";
 var result = {};
 URL.substring(URL.indexOf("?") + 1).split('&').forEach(function(x){
 var arr = x.split('=');
 arr[1] && (result[arr[0]] = arr[1]);
 });
 console.log(result.search_terms);
 //OUTPUT: "Generator+Repair"
 console.log(result.geo_location_terms);
 //OUTPUT: "Adamsville%2C+Alabama"
</script>
answered Nov 12, 2018 at 18:07

Comments

0

You can use the following regex to get the 2 values:

/search_terms=(.*)&geo_location_terms=(.*)/

This is a very basic regex, that starts by matching 'search_terms=' then creates a Group that matches any number of any char up to the '&' sign, then matches 'geo_location_terms=' and finally creates a Group that matches any number of any char.

Your desired output will be in Group 1 and Group 2.

How to use:

var url = 'https://www.yellowpages.com/search?search_terms=Generator+Repair&geo_location_terms=Adamsville%2C+Alabama';
var regex = /search_terms=(.*)&geo_location_terms=(.*)/;
var match = url.match(regex);
var search_terms = match[1];
var geo_location_terms = match[2];
answered Nov 12, 2018 at 18:16

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.