I have the following code:
function strong(){
var element = document.getElementById("decode");
var value = element.textContent || "";
var regex = /\*([a-zA-Z\d\w\s]+)\*/gi;
element.innerHTML = value.replace(regex, "<strong>$1</strong>");
} strong();
<div id="decode">
*Lorem ipsum dolor* sit amet consectetur adipiscing elit Vestibulum aliquet justo id lobortis varius tortor leo tristique nulla volutpat maximus arcu urna viverra nisl Duis nec ultrices lacus.\r\n\r\nCurabitur vehicula convallis diam, a pellentesque velit efficitur ac.
</div>
The function it fulfills is to convert the sentence enclosed in strong, there it works perfectly. What I want is that it also replaces \r\n
with a line break <br/>
but it doesn't work for me, I already tried this way:
function strong(){
var element = document.getElementById("newsfordecostrong");
var value = element.textContent || "";
var regex = /\*([a-zA-Z\d\w\s]+)\*/gi;
element.innerHTML = value.replace(regex, "<strong>$1</strong>");
element.innerHTML = value.replace('\r\n', "<br/>"); // Se supone que aquí se haría el salto de linea.
}
strong();
But it doesn't work either, I appreciate your answers.
You need to escape the
\r
and the\n
, enclose it in a regular expression instead of having it as a string and finally add as a modifierg
so that it replaces all occurrences.Also be careful when you chain replacements, you have to accumulate them in a variable and at the end assign it to
element.innerHTML
. The way you had it, the first replacement was crushed by the second.