简体   繁体   English

正则表达式替换在引号之间输入

[英]Regex replace enters between quotes

How do i replace all enters between two quotes in a text file. 如何替换文本文件中两个引号之间的所有输入。 The first quote is always preceded by a tab or it is the first character in the row (csv file). 第一个引号始终以制表符开头,或者它是行中的第一个字符(csv文件)。 I tried the following regex 我尝试了以下正则表达式

/(\t"|^")([^"]*)(\n)([^"]*")/gm

but this regex only matches the first enter between two quotes, not all. 但是这个正则表达式只匹配两个引号之间的第一个输入,而不是全部。

For example, the following text: 例如,以下文字:

xx "xx 
xx 
xx" 
xx 
"xx"
xx 
xx
"xxx xxx 
xx"

should become 应该成为

xx "xx xx xx" 
xx 
"xx"
xx 
xx
"xxx xxx xx"

I read the following post ( javascript regex replace spaces between brackets ) which is very similar, but the regex suggested there is not useable in my situation. 我阅读了以下帖子( javascript正则表达式替换括号之间的空格 )非常相似,但正则表达式建议我的情况不可用。

With Javascript replace you can use a function as replacement . 使用Javascript替换,您可以使用函数作为替换

 var str = 'foo \\n"a\\n" bar\\n'; str = str.replace(/"[^"]+"/g, function(m) { return m.replace(/\\n/g, ' '); }); console.log(str); 

The regex "[^"]+" will match quoted stuff with one or more non -quotes in between. 正则表达式"[^"]+"将引用的东西与之间的一个或多个引号相匹配。

Add conditions such as tab or start to the pattern as needed: (?:\\t|^)"[^"]+" 根据需要添加选项卡或启动到模式等条件: (?:\\t|^)"[^"]+"

\n(?=[^"]*"(?:[^"]*"[^"]*")*[^"]*$)

You can use this and replace by empty string . 您可以使用它并替换为empty string

See Demo 见演示

var re = /\n(?=[^"]*"(?:[^"]*"[^"]*")*[^"]*$)/g; 
var str = 'xx "xx \nxx \nxx" \nxx \n"xx"\nxx \nxx\n"xxx xxx \nxx"';
var subst = ''; 

var result = str.replace(re, subst);

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

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