Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to correctly extract part of a string js?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

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

>Solution :

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'));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading