1

Tengo el siguiente código:

function strong(){
var element = document.getElementById("deco");
  var value = element.textContent || "";
  var regex = /\*([a-zA-Z\d\w\s]+)\*/gi;
  value = value.replace(regex, "<strong>$1</strong>"); // STRONG

  element.innerHTML = value; 
} strong();
<p id="deco">
*It is a long established* fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution
</p>

La función que cumple es hacer strong las oraciones encerradas entre asteriscos, pero... ¿Cómo puedo convertir las oraciones encerradas en (+) en un link? Ejemplo:

+https://www.miweb.com+ Convertirla a link de este modo: https://www.miweb.com

He probado agregando y sustituyendo las variables del JavaScript de este modo:

function strong(){
    var element = document.getElementById("newsfordecostrong");
    var value = element.textContent || "";
    var regex = /\*([a-zA-Z\d\w\s]+)\*/gi; // STRONG
    var link = /\+([a-zA-Z\d\w\s]+)\+/gi; // LINK
    value = value.replace(regex, "<strong>$1</strong>"); // STRONG
    value = value.replace(link, "<a href='$1'>$1</a>"); // LINK

    element.innerHTML = value; 
} strong();

Pero no funciona, si estoy haciendo algo mal o podría mejorar, agradecería sus comentarios y respuestas.

1 respuesta 1

4

puedes hacerlo asi:

function strong(){
    var element = document.getElementById("newsfordecostrong");
    var value = element.textContent || "";
    var regex = /\*([a-zA-Z\d\w\s]+)\*/gi; // STRONG
    var link = /\+([(http(s)?):\/\/(www\.)?a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b[-a-zA-Z0-9@:%_\+.~#?&//=]*)+\+/gi; // LINK
    
    value = value.replace(regex, "<strong>$1</strong>"); // STRONG
    value = value.replace(link, "<a href='$1'>$1</a>"); // LINK

   element.innerHTML = value; 
} strong();
<p id="newsfordecostrong">
*It is a long established* fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution +https://google.com/+ or +www.google.com+ or +http://google.com/+ or +https://www.google.com/+
</p>

Tu Respuesta

Al pulsar en “Publica tu respuesta”, muestras tu consentimiento a nuestros términos de servicio, política de privacidad y política de cookies

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.