Home > database >  If using javascript convert /n to >
If using javascript convert /n to >

Time:09-16

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 >尚有 1 個職缺使用母公司的點數,\n關閉後才可解除綁定</p>

CodePudding user response:

const demoEl = document.querySelector('.demo')
demoEl.innerHTML = demoEl.textContent.replace(/\\n/g, '<br />');
<p >尚有 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
  • Related