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?

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$/, '')

In Windows you might have \r\n there. So in the core it is often done like that:

clientData.replace(/(\n|\r)+$/, '')

BTW, clientData.trim() function might be useful too.