Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

146
Vistas
How should I insert specific characters to prevent discord formatting?

I am working on a small webpage that formats the text you input, for a Minecraft server I am involved in. The text is then meant to be copied to a discord channel. The problem is Discord's formatting which allows some characters can make the text underlined or bold. Some Minecraft usernames have underscored, and I wanted to make a loop that adds "" in front of any underscores so discord ignores them. This is what I came up with:

// example for result
var result = "__MinecraftUsername__";

// the loop
while (result.includes("_") == true) {
  result = i(result, result.indexOf("_"), "\");
}

// in the actual code this is put into the html
alert(result);

// function to add characters at a specific index
function i(str, index, value) {
    return str.substr(0, index) + value + str.substr(index);
}
When I run this code the webpage becomes unresponsive. I am very new to JavaScript and I am sorry if this is a really silly mistake.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Try using a javascript function like the one in this example... that will eliminate the while loop.

But as a suggestion, why not wrap the minecraft names with back-quotes? That may preserve the name's characters, without trying to treat them as markdown codes. As an example of wrapping with back-quotes preserves the characters that would normally be treated as markdown: __some_*minecraft*_name__.

I'm using jQuery just to display the results, so you can ignore the jQuery.

var mcName = "_some_minecraft_name_";
$(".original").text(mcName);

var results = mcName.replaceAll("_", "\");
$(".sample").text(results);


/**
 * This change to the javascript String prototype adds a replaceAll
 * function to the String.  This prevents the need to do a
 * split().join() explicitly.
 *
 * @param search
 * @param replace
 * @returns
 */
String.prototype.replaceAll = function( search, replace ) {
    var results = this.split(search).join(replace);

    return results;
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p class="original"></p>
<p class="sample"></p>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda