Home > other >  How to correctly extract part of a string js?
How to correctly extract part of a string js?

Time:08-24

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

CodePudding user response:

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'));

  • Related