Splitting a URL by either of 2 different parts

Viewed 59

Currently, my code splits the URL and removes anything after &7. How can I also make it check for |relevance at the same time and split by whichever one it sees?

$(document).ready(($) => {
  const params = new URLSearchParams(window.location.search);
  if (params.has("query")) {
    const query = params.get("query").split('%7').pop();
    $("#textfield").val(query);
  }
});

I tried replacing .split('%7').pop(); with .split('%7' || '|relevance').pop(); but that didn't work.

1 Answers

The or needs to be in regex format.

here is where you can test regex: https://regex101.com/r/kQwpTN/1

const query = params.get("query").split(/%7|\|relevance/).pop();

Cheers!

Related