0

Hello There I have a working Node.js Script but need help splitting on variable which is a string into 2 variables using the : symbol which is in the first variable

  // Configure our HTTP server to respond with Hello World to all requests.
  var server = http.createServer(function (request, response) {
  var queryData = url.parse(request.url, true).query;
  response.writeHead(200, {"Content-Type": "text/plain"});

  if (queryData.name) {
    // user told us their name in the GET request, ex: http://host:8000/?name=Tom
    var exec = require('child_process').exec;

    exec ("casperjs test.js " + queryData.name + '\n',function(err, stdout, stderr) {

        response.end(stdout);

    });

  } else {
    response.end("Contact Admin - Not Working\n");
  }
});

// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8283);

I would like to be able to split queryData.name into something like below

exec ("casperjs test.js " + queryData.name1 + " " + queryData2.name + '\n',function(err, stdout, stderr)

QueryData contains a string like below

127.0.0.1:@mailserver1

I would like to split this so

+ queryData1.name +  =  127.0.0.1
+ queryData2.name +  = @mailserver1 

so this would be using the : as a splitter

Claus Jørgensen
  • 25,882
  • 9
  • 87
  • 150
user3316689
  • 51
  • 1
  • 6
  • Possible duplicate of [How do I split a string, breaking at a particular character?](https://stackoverflow.com/questions/96428/how-do-i-split-a-string-breaking-at-a-particular-character) – Claus Jørgensen Sep 16 '18 at 17:24

2 Answers2

0

"127.0.0.1:@mailserver1".split(':')[1];

C B
  • 12,482
  • 5
  • 36
  • 48
  • I think you mis under stood I need to split + queryData.name + into to variables + queryData1.name + and + queryData2.name + that above that you used is just an example of the data inside + queryData.name + – user3316689 Feb 16 '14 at 18:38
  • can you correct the answer I need to split queryData into 2 variables – user3316689 Feb 16 '14 at 18:48
0

If you're coding to the ES6 spec:

const s = "127.0.0.1:@mailserver1";
const [ip, server] = s.split(":");
console.log(`IP: ${ip}, Server: ${server}`);
Tom O.
  • 5,730
  • 2
  • 21
  • 35