Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove new line from user console input

How can I remove new line from the user input in Node.js?

The code:

var net = require("net");

var clientData = null;

var server = net.createServer(function(client) {
    client.on("connect", function() {
        client.write("Enter something: ");
    });
    client.on("data", function(data) {
        var clientData = data;
        if (clientData != null) {
            client.write("You entered " + "'" + clientData + "'" + ". Some more text.");
        }
    });
});

server.listen(4444);

Let's say I type "Test" in the console, then the following is returned:

You entered 'Test
'. Some more text.

I would like such an output to appear in the single line. How can I do this?

like image 446
Eleeist Avatar asked May 10 '12 16:05

Eleeist


1 Answers

You just need to strip trailing new line.

You can cut the last character like this :

clientData.slice(0, clientData.length - 1)

Or you can use regular expressions :

clientData.replace(/\n$/, '')
like image 197
kevin Avatar answered Oct 07 '22 11:10

kevin