首页 \ 问答 \ 正则表达式(C#):用\ r \ n替换\ n(Regex (C#): Replace \n with \r\n)

正则表达式(C#):用\ r \ n替换\ n(Regex (C#): Replace \n with \r\n)

如何使用C#中的正则表达式将\ n的单独实例替换为\ r \ n(仅LF与CRLF)?

对不起,如果这是一个愚蠢的问题,我是新来的正则表达式。

我知道要使用计划String.Replace来做,如:

myStr.Replace("\n", "\r\n");
myStr.Replace("\r\r\n", "\r\n");

然而,这是不雅的,并且会破坏文本中的任何“\ r + \ r \ n”(尽管它们不可能存在)。


How can I replace lone instances of \n with \r\n (LF alone with CRLF) using a regular expression in C#?

Sorry if it's a stupid question, I'm new to Regex.

I know to do it using plan String.Replace, like:

myStr.Replace("\n", "\r\n");
myStr.Replace("\r\r\n", "\r\n");

However, this is inelegant, and would destroy any "\r+\r\n" already in the text (although they are not likely to exist).


原文:https://stackoverflow.com/questions/31053
更新时间:2023-08-21 14:08

最满意答案

我们很难描述一个通用的策略来调试“很多错误”,所以我将引导您完成调试组件级错误的过程:

  1. 为您所选择的浏览器安装React开发工具扩展:

  2. 找到组件

    我推荐以下任一路径:

    2.A. 从浏览器开发工具中打开“ React tab ,然后键入底部搜索栏以按名称查找组件。

    2.B. 打开Elements tab ,选择一些元素,然后切换回React 。 组件层次结构将被展开,直到React组件匹配您在Elements选择的DOM节点。

  3. 检查行为不正确的组件的状态和道具。

如果由于某种原因你不喜欢安装浏览器扩展,你可以抛出一个或多个debugger; 语句来设置将“暂停”应用程序执行的断点,并让您检查这些点的调用堆栈,范围和错误。


It's difficult to describe a general strategy to debug "a lot error shown up" so I will walk you through the process for debugging component level errors:

  1. Install the React dev tools extension for your browser of choice:

  2. Find the component

    I recommend either of these paths:

    2.a. Open React tab from your browser dev tools and type in the bottom search bar to lookup components by name.

    2.b. Open Elements tab, select some element, and switch back to React. The component hierarchy will be expanded up until the React component matching the DOM node you selected in Elements.

  3. Inspect the state and props of the component that is behaving incorrectly.

If for some reason you don't like installing browser extensions, you can throw one or more debugger; statements to set breakpoints that will "pause" the execution of your app and let you inspect the call stack, scope, and errors at these points.

相关问答

更多
  • 我们很难描述一个通用的策略来调试“很多错误”,所以我将引导您完成调试组件级错误的过程: 为您所选择的浏览器安装React开发工具扩展: 铬 火狐 找到组件 我推荐以下任一路径: 2.A. 从浏览器开发工具中打开“ React tab ,然后键入底部搜索栏以按名称查找组件。 2.B. 打开Elements tab ,选择一些元素,然后切换回React 。 组件层次结构将被展开,直到React组件匹配您在Elements选择的DOM节点。 检查行为不正确的组件的状态和道具。 如果由于某种原因你不喜欢安装浏览器扩 ...
  • Chrome不允许您通过XHR加载file:// urls(其他地方如何在浏览器转换中工作)。 你有几个选择: 使用不同的浏览器。 我知道Firefox工程。 启动一个本地的Web服务器(Python附带一个,所以如果你已经安装了这个非常简单 - http://www.linuxjournal.com/content/tech-tip-really-simple-http-server-python )。 将脚本放在一个单独的文件中。 这对于像这样简单的东西是可行的,但是您会想要尝试其他选项,因为您的代码变 ...
  • 有几个基本原则要牢记,可能有助于您构建一个很好的React应用程序: 您的UI应该是数据的一个功能 在许多“jQuery汤”风格的应用程序中,应用程序的业务逻辑,应用程序的数据和UI交互代码都是混合的。 这使得这些应用程序难以进行调试,特别是难以增长。 像许多现代客户端应用程序框架一样,React实现了一个想法,即UI只是您的数据的表示。 如果您希望您的UI更改,您应该更改一个数据,并允许框架用于更新UI的任何绑定系统。 在React中,每个组件(理想情况下)是传递给组件实例的属性以及组件在内部管理的状态的 ...
  • 所以我按照本教程获取导航链接以转到页面的其他部分。 我没有使用this.props.children将组件传递给layout.js文件,而是在单个页面上添加了我想要的每个组件,如下所示: return (
  • 所以我们终于搞清楚了。 我们包装了用jQuery编写的Golden layout并创建了react模块。 一切都像魅力一样。 还有可能将我们的React组件包装为jQuery模块,如果你想了解更多,请遵循本教程 。 So we finally figured it out. We wrapped Golden layout which is written in jQuery and created react modules. Everything is working like a charm. The ...
  • 好问题! 你问的是正确的事情,我可以告诉你,你将在短时间内掌握React。 您似乎可以使用一些帮助来更好地理解组件生命周期和单向数据绑定的美感。 我将列出一些您可能会找到帮助的资源,我会尽力回答您下面的具体问题。 这是关于组件生命周期的好文章: http://busypeoples.github.io/post/react-component-lifecycle/ 另一个很好的资源/工具是Redux。 它不仅是一个很好的调试工具,而且还可以帮助您学习,因为它可以更容易地可视化幕后发生的事情。 Redux: ...
  • “import”来自JavaScript ES6,我强烈推荐使用它 - 与babel一起使用 。 您将找到许多样板项目,只需谷歌搜索“反应es6样板”,例如: https : //github.com/vasanthk/react-es6-webpack-boilerplate 有一个关于如何在另一个样板中使用组件的简单示例: https : //github.com/lalitkapoor/webpack-es6-react-boilerplate/tree/master/public/js "impor ...
  • 您需要安装一个识别JSX的插件。 巴贝尔崇高是伟大的。 一旦你安装了,你可以将你的语法设置为Babel(JavaScript)而不仅仅是JavaScript。 这里有一篇关于为React.js设置Sublime的博客文章。 它向您展示了如何将默认语法突出显示设置为babel。 在教程中它为.jsx扩展做了它,但是我为.js做了它,并且它适用于所有JavaScript。 http://cheng.logdown.com/posts/2015/07/10/setup-sublime-text-3-for-rea ...
  • 您可以从Github下载的zip,其中提到的React示例包括各种语言的服务器脚本,这些脚本将处理示例中的JSON数据更新。 因此,如果您下载并使用其中一个服务器脚本在本地运行示例,您将看到您的代码将起作用。 ajax调用会将您的注释数据发送到指定的url,服务器脚本将负责更新JSON注释文件。 HTH The zip you can download from Github for the React example you mention includes server scripts for a va ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)