How to extract a IP address from JavaScript string data [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
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--
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?

One of the most simple regex pattern would be:
const 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"}]}`
const reg = new RegExp(/..\....\...\..../g)
const res = aa.match(reg)
console.log(res[0]);
but as posted in comments why not use JSON.parse?

Parse the string with JSON.parse(STRING) then access your network object ("PC-lab-network-452") JSON.parse(aa)["PC-lab-network-452"] then access any valid array index JSON.parse(aa)["PC-lab-network-452"][0] then access the addr property JSON.parse(aa)["PC-lab-network-452"][0].addr

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

Related

Converting a javascript string into an existing javascript variable [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am stuck with this problem. I have a function to accept the path and also the same time I have a variable that I want to condition with.
Here is my problem: I want to make a string type that will act as an access to my variable.
In my situation, I have a roles.operation variable which I want to access it dynamically.
The roles variable has an array with the values of:
roles.operations = ['document','article','document-type'];
with this variable I want this to be access dynamically.
Here is what I've tried, which in replacePath i have the value of document-type:
export const createVariable = (roles,path) => {
const replacePath = path.replace(/-/g,"_");
const finalPath = window[`roles.operations.${replacePath}`];
console.log(finalPath);
}
this gives me undefined.
Try this way:
const finalPath = window['roles']['operations'][replacePath];
or
const finalPath = window.roles.operations[replacePath];

Javascript: How to parse data from this string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have the following string returned from a server:
{"href":"about:blank#&executeFunction<-finishedStroke&&arguments<-{\"base64DataUrl\":\"data:image/png;base64,iVBORw0KGgoAAErkJggg==\"}&"}
I want to extract data:image/png;base64,iVBORw0KGgoAAErkJggg== (the base64DataURL part) from it. What's the best way to do this with Javascript?
Please note that I am asking how to parse values from JSON. This is not the same as this question, which asks how to extract fields from nested objects and arrays.
What I've tried: I have tried
JSON.parse(<string>).href.split('base64DataUrl":"')[1].split('"')[0]
which yields the right answer, but I'm hoping for a more concise solution.
This is JSON, but the part you're interested in isn't exposed as part of the object. So I would suggest that you just consider it a string and use a string-parsing method, like a regular expression.
const string = `{"href":"about:blank#&executeFunction<-finishedStroke&&arguments<-{\"base64DataUrl\":\"data:image/png;base64,iVBORw0KGgoAAErkJggg==\"}&"}`;
const dataURL = /"base64DataUrl":"(.*?(?="))/gm.exec(string)[1];
console.log(dataURL);
Since the server is not returning valid JSON, you will have to parse it with indexOf and substring.
let y = stringFromServer;
let a = y.indexOf('data:');
let b = y.indexOf('\"}',a);
let dataUrl = y.substring(a,b);
Here is my try
let sample = {"href":"about:blank#&executeFunction<-finishedStroke&&arguments<-{\"base64DataUrl\":\"data:image/png;base64,iVBORw0KGgoAAErkJggg==\"}&"}
let splitter = sample.href.split("<-")
console.log(JSON.parse(splitter[2].substring(0, splitter[2].length - 1))["base64DataUrl"]
)

Returning query fragments [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have an url that looks like this:
https://example.com/?category=123&dk=sports&dk=groupcompanyinsider&dk=local&lang=en
Is it possible to return every dk parameter separately? (no matter if there will be 1 or 5 dk parameters) so i would get separately sports, groupcompanyinsider, local.
If its not possible maybe there is a way to return all of them in one string like dk=sports&dk=groupcompanyinsiderlocal&dk=local ?
You can use the built-in javascript class URLSearchParams for this.
You can then transform this into the string you want with string concatenation and a foreach.
const url = "https://example.com/?category=123&dk=sports&dk=groupcompanyinsider&dk=local&lang=en";
var params = new URLSearchParams(url);
var result = "";
// concatenate individual values of the 'dk' query parameter
params.getAll('dk').forEach(function (item) {
result += '&dk=' + item;
});
result = result.substr(1); // remove starting '&' from the result;
console.log(result);
The result should contain your desired string.

Find the string is XML or JSON or String using angular 2 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I need to find my string is XML or JSON object or string, based on this response I will make the string looks pretty.
So for that, I need to identify what kind of object. Can anyone please help me to find a way to achieve this?
Thanks in advance.
try like this
function GetInputType(response) {
try {
//try to parse via json
a = JSON.parse(response);
return 'json type';
} catch(e) {
//try xml parsing
let parser = new DOMParser;
var xmlDoc = parser.parseFromString(response,"application/xml");
if(xmlDoc.documentElement.nodeName == "parsererror")
return 'string type';
else
return 'xml type';
}
}

How to do array from the string ? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have an adress such as https://stackoverflow.com/#/schedulePage/some/another , how can I make it to an array with elements after # ?
var urlToSplit = "https://stackoverflow.com/#/schedulePage/some/another"
var onlyAfterHash = urlToSplit.split("/#/")[1];
var result = onlyAfterHash.split("/");
console.log(result);
Use the split function
var str = "https://stackoverflow.com/#/schedulePage/some/another";
var res = str.split("#");
I think you just want something like this.First here I just split out the string by #, grab the second part of split result i.e index 1 then again splitting the result with / and finally filter out the empty string from the generated array.
var string = 'https://stackoverflow.com/#/schedulePage/some/another';
var result = string.split('#')[1].split('/');
var filtered = result.filter((entry)=>entry.trim() != '');
console.log(filtered);

Categories

Resources