Why can't I replace a newline Char with Typescript (Javascript) - Stack Overflow

I have a field where the user can enter some text that will be shown in another div on the page. It als

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

Share Improve this question edited Jan 5, 2018 at 15:26 Mischa asked Oct 13, 2017 at 11:58 MischaMischa 1,33312 silver badges24 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

Try 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745475375a4629323.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信