I want remove the duplicate word with regex
let url = "/courses?category=programming&filter=free&filter=free"
const az = url.replace(/(\b\S.+\b)(?=.*\1)/g, "").trim();
console.log(az) // /courses?category=programmingfree&filter=free
I want to get /courses?category=programming&filter=free
You can use the URLSearchParams constructor to parse the URL parameters, Object.fromEntries to convert it to an object (and remove duplicate keys), then parse back into an URLSearchParams object, from which you can call toString() to get the result:
let url = "/courses?category=programming&filter=free&filter=free"
let [path, params] = url.split("?");
let result = path + '?' + new URLSearchParams(Object.fromEntries(new URLSearchParams(params))).toString()
console.log(result)