0

Hello friends i have website url ="https://www.example/en_uk/cgid=ladywears&middle=20&end=100" I need to extract the value of parameter "middle" and "end" which can be any numeric value from 10 to 10000. Have 1 condition which need to fulfilled -- Url will be passed dynamically and the program should only run if it has exact "middle" and "end" word matching in it .Need help in writing it in JavaScript .

Thanks in advance

Sachin
  • 19
  • 1
  • 3

1 Answers1

1

You can use URLSearchParams to parse the params.

In your real environment, you must use var url = window.location.search; to get the current URL. In the below example, I just use your provided URL instead of window.location.search;.

// Use window.location.search in your live environment
// var url = window.location.search;

var url = "https://www.example/en_uk/cgid=ladywears&middle=20&end=100";
const urlParams = new URLSearchParams(url);
const middle = urlParams.get('middle');
const end = urlParams.get('end');

console.log('middle', middle);
console.log('end', end);
Tuan Dao
  • 2,647
  • 1
  • 10
  • 20
  • Thanks but is there any way by which we can split the url and extract the value of end and middle . – Sachin Sep 23 '21 at 17:50