mert cihan - 1 year ago 87

Node.js Question

I am new to regex. I have been stuck with this and couldn't find a way to figure this out.

I am using a nodejs in back-end. There is an output to clear things up with regex,

`results: ["Comparing /path/uploads/vector-1474379968511.data with /path/x1.data."," + Squared l2 distance between representations: 2.067",`

"Comparing /path/uploads/vector-1474379968511.data with /path/x2.data."," + Squared l2 distance between representations: 1.670",

"Comparing /path/uploads/vector-1474379968511.data with /path/x3.data."," + Squared l2 distance between representations: 1.686",

"Comparing /path/uploads/vector-1474379968511.data with /path/x4.data."," + Squared l2 distance between representations: 0.287"]

I need the path ( "/path/x1.data", "/path/x2.data", etc. ) for all x1.data, x2.data, etc. For each x1.data, x2.data, ... there is a corresponding "representations" value. Paths need to have these values. In other words, I need the following:

/path/x1.data => 2.067

/path/x2.data => 1.670

/path/x3.data => 1.686

.................... => ......

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

```
var input = ["Comparing /path/uploads/vector-1474379968511.data with /path/x1.data.", " + Squared l2 distance between representations: 2.067",
"Comparing /path/uploads/vector-1474379968511.data with /path/x2.data.", " + Squared l2 distance between representations: 1.670",
"Comparing /path/uploads/vector-1474379968511.data with /path/x3.data.", " + Squared l2 distance between representations: 1.686",
"Comparing /path/uploads/vector-1474379968511.data with /path/x4.data.", " + Squared l2 distance between representations: 0.287"];
var output = {};
var findPath = / with (.*)\.$/, findValue = /(\d+\.\d+)$/;
var path, value;
for (var i = 0, l = input.length; i < l; i++) {
path = input[i].match(findPath)[1];
i++; // move to next item
value = input[i].match(findValue)[1];
output[path] = value; // store however you like
}
console.log(output);
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**