正则表达式替换在引号之间输入 [英] Regex replace enters between quotes

查看:104
本文介绍了正则表达式替换在引号之间输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何替换文本文件中两个引号之间的所有输入。第一个引号始终以制表符开头,或者它是行中的第一个字符(csv文件)。我尝试了下面的正则表达式

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). I tried the following regex

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

但这个正则表达式只匹配两个之间的第一个输入报价,不是全部。

but this regex only matches the first enter between two quotes, not all.

例如,以下文字:

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

应该成为

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

我阅读了以下帖子(
javascript正则表达式替换括号之间的空格)这非常相似,但正则表达式建议在我的情况下不可用。

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.

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.

根据需要添加选项卡或开始条件等条件: (?:\t | ^)[^] +

Add conditions such as tab or start to the pattern as needed: (?:\t|^)"[^"]+"

这篇关于正则表达式替换在引号之间输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆