admin管理员组文章数量:1426510
I have a field where the user can enter some text that will be shown in another div on the page. It also get saved to the database (MSSQL).
When refreshing the page the saved text will be loaded and can be edited. As soon as the text is edited the div will also be live updated. To make linebreaks working i made a quick linebreak replacement method that should cover all cases of linebreaks.
let content = Tooltip.GetText(); // Tooltip is a Devexpress Memo Control
let content1 = content.replace("\r\n", "<br />");
let content2 = content1.replace("\n\r", "<br />");
let content3 = content2.replace("\n", "<br />");
let content4 = content3.replace("\r", "<br />");
While this will work for linebreaks that got loaded from the database it will not work for linebreaks the user just entered. However, the linebreak will work after a refresh of the page.
Here is a screenshot of my current debugging result. You can clearly see that the first of the two linebreaks got replaced but not the second one. I want to understand why.
I tried to copy the debugging content from the console to Notepad++ to have a look at the specific character, but it looks like that Notepadd++, Windows or Chrome change the data when copying it. Notepad++ will show for both \n\r
I have a field where the user can enter some text that will be shown in another div on the page. It also get saved to the database (MSSQL).
When refreshing the page the saved text will be loaded and can be edited. As soon as the text is edited the div will also be live updated. To make linebreaks working i made a quick linebreak replacement method that should cover all cases of linebreaks.
let content = Tooltip.GetText(); // Tooltip is a Devexpress Memo Control
let content1 = content.replace("\r\n", "<br />");
let content2 = content1.replace("\n\r", "<br />");
let content3 = content2.replace("\n", "<br />");
let content4 = content3.replace("\r", "<br />");
While this will work for linebreaks that got loaded from the database it will not work for linebreaks the user just entered. However, the linebreak will work after a refresh of the page.
Here is a screenshot of my current debugging result. You can clearly see that the first of the two linebreaks got replaced but not the second one. I want to understand why.
I tried to copy the debugging content from the console to Notepad++ to have a look at the specific character, but it looks like that Notepadd++, Windows or Chrome change the data when copying it. Notepad++ will show for both \n\r
1 Answer
Reset to default 6Try with this:
let content1 = content.replace(/\r\n/g, "<br />");
let content2 = content1.replace(/\n\r/g, "<br />");
let content3 = content2.replace(/\n/g, "<br />");
let content4 = content3.replace(/\r/g, "<br />");
Your problem is that .replace
function replaces just the first occurrence unless you pass a regex with the global flag.
本文标签: Why can39t I replace a newline Char with Typescript (Javascript)Stack Overflow
版权声明:本文标题:Why can't I replace a newline Char with Typescript (Javascript) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745475375a2659939.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论