-1

I have a URL and I need to isolate a certain value from it, and to be more precise, the value after numberType=.

There may or may not be an ampersand after this value. How can I implement this with a generic function?

Examples

http://localhost:8080/type/3259/?filters=901&numberType=77

http://localhost:8080/type/3259/?filters=901&numberType=77&somethingElse=string
Mureinik
  • 297,002
  • 52
  • 306
  • 350

1 Answers1

1

Let the URL class do the heavy lifting for you:

const input = 'http://localhost:8080/type/3259/?filters=901&numberType=77';
const parsed = new URL(input);
console.log(parsed.searchParams.get('numberType'));
Mureinik
  • 297,002
  • 52
  • 306
  • 350