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 cut part of string in vanilla js to send like as parameter on API

I have a little problem with this url.

I want to get only the numbers on the first drop down and send it like as paramater on API.

dropdown

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

I create a object with properties:

const dataAPI = { nameStation: '', nameStationNumber: '', dateStation: '', dataHours: '' }

and I want dataAPI.nameStationNumber to save only a number of clicked value.

With this code I get only this string for example:

Chepelarska - 18694 - HPoint-Water level 

dataAPI.nameStation = e.target.innerHTML.replace(/.*\+\s?/, '');

How can I cut only 18694 from река Чепеларска - гр. Асеновград + Chepinska - 10084 - HPoint-Water level

Can I get example how to do that ?

>Solution :

Use regex /[^\d]+/ (replace [^] anything that does not match \d (number))

let string = 'река Чепеларска - гр. Асеновград + Chepinska - 10084 - HPoint-Water level';
let result = string.replace(/[^\d]+/g, '');
console.log(result);
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