Home > Net >  How to replace §* to a blank in NodeJS
How to replace §* to a blank in NodeJS

Time:09-13

I have been using RCON to connect to my game server (Minecraft) remotely without logging into my computer and certain commands use § and a number for a color coding in the game and I was wondering if with the output if I could have it removed as the output comes back. Here is the code,

const readline = require('readline').createInterface({
  input: process.stdin,
  output: process.stdout
});
const Rcon = require('rcon') 
const conn = new Rcon('IP', Port, 'password');

conn.on('auth', function() {
  // You must wait until this event is fired before sending any commands,
  // otherwise those commands will fail.
  console.log("Authenticated");
  console.log("Sending command upon entry")
  readline.question('enter command: ', input => {
  conn.send(`${input}`);
  readline.close();
});
}).on('response', function(str) {
  console.log("Response: "   str);
}).on('error', function(err) {
  console.log("Error: "   err);
}).on('end', function() {
  console.log("Connection closed");
  process.exit();
});

conn.connect();

CodePudding user response:

String.prototype.replace

const string = 'string with §and other characters'
console.log(string.replace('§', ''))

CodePudding user response:

If you have only 1 § in string or want to replace only 1st:

string.replace('§', '');

If several, you can either split string by character & unite back to string with empty string or anything else:

string.split('§').join('')

Or use regex to find all appearances and replace with replace function (g in end is flag meaning "global" so it will find all matches):

string.replace(/§/g,'')

With regex replace you can use any regex you want, so may not only clean strings from § but anything, i would suggest to try out building your regex at https://regex101.com/

  • Related