0

I have a link similar to https://app.ticketmaster.com/discovery/v2/events?apikey=xxxxxx&keyword=Sarah%20Jarosz&latlong=xx.xxxxx,-xxx.xxxx&radius=15&unit=miles&locale=* that is being passed as a parameter in a function of mine. I want to get the value of the keyword portion of this link and I was hoping to find something easier than writing my own function with substring.

I am familiar with the window.locaiton.____ method of getting portions of links but this is being passed as a parameter in a function so I cannot use that method. I tried using the method defined in this question Parse an URL in JavaScript but I cannot use .keyword instead of .search to get the keyword portion only.

Jon Walzer
  • 65
  • 1
  • 6
  • 1
    Welcome to Stack Overflow! Please take the [tour] (you get a badge!), have a look around, and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) I also recommend Jon Skeet's [Writing the Perfect Question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/). Please [**search thoroughly**](/search?q=%5Bjs%5D+parse+url) before posting, [the linked question](https://stackoverflow.com/questions/979975/get-the-values-from-the-get-parameters-javascript) was literally the first hit. – T.J. Crowder Nov 25 '21 at 18:31

1 Answers1

1

Use the URL and URLSearchParams apis

const urlString =' https://app.ticketmaster.com/discovery/v2/events?apikey=xxxxxx&keyword=Sarah%20Jarosz&latlong=xx.xxxxx,-xxx.xxxx&radius=15&unit=miles&locale='


function getKeyword (str) {
  const url = new URL(str);  
  return url.searchParams.get('keyword');
}

console.log(getKeyword(urlString))
charlietfl
  • 170,828
  • 13
  • 121
  • 150