简体   繁体   English

RegEx在双引号之间找到换行符并替换为空格

[英]RegEx find newline between double quotes and replace with space

I need help to replace a newline with space in a string with double quotes and value between. 我需要帮助,以双引号和介于两者之间的值的字符串中的空格替换换行符。

This is my current expression but it's only replace newline if string doesn't contain any other value. 这是我当前的表达式,但是仅在string不包含任何其他值时才替换换行符。

/"([\r\n]*?)"/g

I want to change this: 我要更改此:

  • "Log field with multiple lines. “用多行记录字段。

    This is now fixed." 现在,此问题已解决。”

For this: 为了这:

  • "Log field with multiple lines. This is now fixed." “日志字段有多行。现在已修复。”

To remove all chunks of line break chars in between double quotation marks, you need to match these substrings between the qualifying start and end double quotation marks. 要删除双引号之间的所有换行符,请在合格的双引号和结束双引号之间使这些子字符串匹配。 Thus, it is crucial to know if " chars can appear escaped in between the " delimiters. 因此,关键的是要知道,如果"字符可以在之间显示逃过"分隔符。

In a CSV, the literal double quotation marks are usually doubled. 在CSV中,文字双引号通常会加倍。 Then, you may use 然后,您可以使用

 var s = '"Cell1","Cell\\r\\n#2","""Cell #3\\r\\nhere\\nand there"""'; s = s.replace(/"(?:""|[^"])+"/g, function(x) { return x.replace(/[^\\S\\r\\n]*[\\n\\r]\\s*/g, ' ');}); console.log(s); 

The "(?:""|[^"])+"/g regex matches a " , then 1 or more occurrences of "" substring or any char other than " , and then " . "(?:""|[^"])+"/g正则表达式匹配" ,然后出现1次或多次""子字符串或除"以外的任何字符,然后匹配" " When the match is found, all CR and LF symbols with any 0+ whitespaces before and after them are removed using a simple .replace(/[^\\S\\r\\n]*[\\n\\r]\\s*/g, ' ') replace operation. 找到匹配项后,使用简单的.replace(/[^\\S\\r\\n]*[\\n\\r]\\s*/g, ' ')删除之前和之后带有0+空格的所有CR和LF符号.replace(/[^\\S\\r\\n]*[\\n\\r]\\s*/g, ' ')替换操作。

If the literal double quotation marks are escaped with a backslash, you may use 如果文字双引号以反斜杠转义,则可以使用

/"[^"\\]*(?:\\[\s\S][^"\\]*)*"/g

If you are sure there are no escaped double quotation marks, use 如果您确定没有转义的双引号,请使用

/"[^"]+"/g

This should solve your problem: 这应该可以解决您的问题:

/\r\n|\r|\n/g

1st Alternative \\r\\n 第一种替代方案\\ r \\ n

\\r matches a carriage return (ASCII 13) \\n matches a line-feed (newline) character (ASCII 10) \\ r匹配回车符(ASCII 13)\\ n匹配换行符(换行符)(ASCII 10)

2nd Alternative \\r 第二替代\\ r

\\r matches a carriage return (ASCII 13) \\ r匹配回车符(ASCII 13)

3rd Alternative \\n 第三选择\\ n

\\n matches a line-feed (newline) character (ASCII 10) \\ n与换行符(ASCII 10)匹配

g modifier g修饰符

All matches (don't return after first match) 所有比赛(第一次比赛后不返回)

You could use a function within .replace() like this: 您可以在.replace()使用如下函数:

 var data = ` I need help to replace a newline with space in a string with double quotes and value between. This is my current expression but it's only replace newline if string doesn't contain any other value. I want to change this: "Log field with multiple lines. This is now fixed." For this: "Log field with multiple lines. This is now fixed." `; var regex = /"[^"]*"/g; data = data.replace(regex, function (match, capture) { return match.replace(/[\\n\\r]\\s*/g, " "); }); console.log(data); 

First, it looks for anything bewteen dobule quotes, second is removes newlines and possible consecutive whitespaces thereafter. 首先,它查找在双引号之间的所有内容,其次是删除换行符,然后删除可能的连续空格。 The approach won't work with escaped quotes though. 但是,该方法不适用于转义引号。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM