-1
 "?page=2&pageSize=12&query=hex"

I have the above text I want to remove page and pageSize along with value. After removing the page and pageSize my text should look like given below.

"?query=hex"
Jitendra Rathor
  • 607
  • 8
  • 11

5 Answers5

2

You can use URLSearchParams to remove params of the query:

const queryString = "?page=2&pageSize=12&query=hex";
const searchParams = new URLSearchParams(queryString);

searchParams.delete("page");
searchParams.delete("pageSize");

const resultQuery = `?${searchParams.toString()}`;

Reference: https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams

ljbc1994
  • 2,044
  • 11
  • 13
1

here is my solution

text.replace(/(page=\d+&?)|(pageSize=\d+&?)/g, '')
TopW3
  • 1,477
  • 1
  • 8
  • 14
0

Here's one way of doing it without any third-party library:

const input = "?page=2&pageSize=12&query=hex"

const parts = input
  .replace(/^\?/, "")
  .split("&")
  .filter(p => !/^(page|pageSize)/.test(p))

const output = "?" + parts.join("&")

console.log(output)
sdgluck
  • 24,894
  • 8
  • 75
  • 90
0

Using regex you can do that as below:

const str =  "?page=2&pageSize=12&query=hex";

console.log(str.replace(/page.*&/g, ""));
Rahul Kumar
  • 3,009
  • 2
  • 16
  • 22
0

Here, maybe this will help. Simple to implement and understand.

var str = "?page=2&pageSize=12&query=hex";
var finalStr = '?' + str.substring(str.indexOf('query='))
console.log(finalStr)
Thom A
  • 88,727
  • 11
  • 45
  • 75
Tarun Dadlani
  • 152
  • 3
  • 10