将一个变量字符串拆分为两个

时间:2014-02-16 18:33:02

标签: javascript node.js

你好我有一个工作的Node.js脚本,但需要帮助将变量分割为2个变量,使用:符号在第一个变量中

  // 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);

我希望能够将queryData.name拆分成下面的内容

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

QueryData包含如下字符串

127.0.0.1:@mailserver1

我想分开这个

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

所以这将使用:作为分离器

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

如果您要按照ES6规范进行编码:

const s = "127.0.0.1:@mailserver1";
const [ip, server] = s.split(":");
console.log(`IP: ${ip}, Server: ${server}`);