Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

If using javascript convert /n to >

I have a piece of text that comes from the backend with /n string in it, but I hope I can replace this with
a lot of ways I found on the Internet, but I still can’t replace it with a label smoothly, does anyone know the reason Caused?

Thanks for your help.

let demo = document.querySelector('.demo').textContent;
console.log(demo)
demo.replace(new RegExp('\r?\n','g'), '<br />');
<p class="demo">尚有 1 個職缺使用母公司的點數,\n關閉後才可解除綁定</p>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

const demoEl = document.querySelector('.demo')
demoEl.innerHTML = demoEl.textContent.replace(/\\n/g, '<br />');
<p class="demo">尚有 1 個職缺使用母公司的點數,\n關閉後才可解除綁定</p>

A couple of points:

  1. Your text doesn’t include a new line character so you need to match for the character combination of \n like so… /\\n/g
  2. Your just replacing the content reference, you need to apply the replace to the elements innerHTML
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading