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 extract a IP address from JavaScript string data

I have string data aa = {"PC-lab-network-452":[{"version":4,"addr":"10.186.32.137","OS-EXT-IPS:type":"fixed","OS-EXT-IPS-MAC:mac_addr":"fa:16:3e:39:38:ac"}]}

in javaScript and I’ve to extract the exact IP address –10.186.32.137 from this data

I’m trying this command–

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

b = aa.match(\10.186.32.137\g) but it also matches the pattern like 10.186.32.13. I need to match the exact pattern. Any help to fix this?

>Solution :

If you want to solve this without regex. Try this :

const a = {"PC-lab-network-452":[{"version":4,"addr":"10.186.32.137","OS-EXT-IPS:type":"fixed","OS-EXT-IPS-MAC:mac_addr":"fa:16:3e:39:38:ac"}]};

Object.keys(a).forEach(item => {
    const ipExist = a[item].find(obj => obj.addr === "10.186.32.137");
    if (ipExist) {
        console.log(ipExist.addr);
    }
    else {
        console.log('IP not found');
    }
});
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