Regex replacement comes in between quotation marks

How to replace all incoming between two quotation marks in a text file. The first quote always precedes the tab, or it is the first character in the string (csv file). I tried the following regex

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

but this regular expression matches only one entry between two quotation marks, 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 replaces spaces between brackets ), which is very similar, but the regex suggested that it is not used in my situation.

+4
source share
2 answers

With Javascript replacement you can use the function as a replacement .

var str = 'foo \n"a\n" bar\n';

str = str.replace(/"[^"]+"/g, function(m) {
 return m.replace(/\n/g, ' ');
});

console.log(str);
Hide result

"[^"]+" non -quotes .

, : (?:\t|^)"[^"]+"

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

empty string.

.

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);
+2

All Articles