简体   繁体   English

为什么我不能用Typescript(Javascript)替换换行符Char

[英]Why can't I replace a newline Char with Typescript (Javascript)

I have a field where the user can enter some text that will be shown in another div on the page. 我有一个字段,用户可以在其中输入一些文本,这些文本将在页面上的另一个div中显示。 It also get saved to the database (MSSQL). 它还会保存到数据库(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. 文本一旦被编辑,div也将实时更新。 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 ++,以查看特定字符,但是看起来Notepadd ++,Windows或Chrome在复制时会更改数据。 Notepad++ will show for both \\n\\r Notepad ++将同时显示\\n\\r

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. 您的问题是,除非您使用全局标志传递正则表达式,否则.replace函数将仅替换第一个匹配项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在IE8中使用JavaScript替换换行符? - How can I replace newline characters using javascript in IE8? 为什么我的 JavaScript 替换功能无法正常工作? - Why can't I get my JavaScript replace function to work? 为什么我不能使用Webpack和很棒的Typescript加载器来获取此应用程序来将Typescript文件编译为javascript? - Why can't I get this app to complie the typescript files into javascript using webpack and awesome typescript loader? JavaScript 如何用 2 个新字符替换字符串中的字符 - JavaScript How can I replace a char from a String with 2 new chars JavaScript-如何将某些字符替换为函数中的字符串 - JavaScript - How can I replace string with certain char in to a function 为什么 string.replace(/./gm,function(s){...} 无法在 javascript 中使用“反引号”删除多行字符串中的换行符 - Why string.replace(/./gm,function(s){…} can not remove newline in multiline string with `backticks` in javascript 用JavaScript中的换行符替换空格? - Replace Space with Newline in JavaScript? JavaScript正则表达式替换换行符 - Javascript regexp replace newline 字符串中的字符串不能用换行符分割 - String within a string can't be split by newline char 我无法用javascript和regex正确替换 - I can't replace properly with javascript and regex
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM