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.
For example, the following text:
xx "xx
xx
xx"
xx
"xx"
xx
xx
"xxx xxx
xx"
should bee
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.
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.
For example, the following text:
xx "xx
xx
xx"
xx
"xx"
xx
xx
"xxx xxx
xx"
should bee
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.
Share Improve this question edited May 23, 2017 at 12:09 CommunityBot 11 silver badge asked Jun 7, 2016 at 9:13 NebuNebu 1,7931 gold badge19 silver badges37 bronze badges 4- Which language is this? Javascript? Also, if you have a CSV file, use a CSV parser. – Tomalak Commented Jun 7, 2016 at 9:22
-
1
A regular expression to handle that at once will probably bee very ugly and slow. Consider a multi-pass approach: 1. extract all quoted texts; 2. replace all
\n
in the quoted texts; 3. reassemble the non-quoted parts with the corrected quoted parts. – Good Night Nerd Pride Commented Jun 7, 2016 at 9:27 - @Tomalak I updated the question, javascript is fine. I am using a csv parser but this parser is giving an error because of an enter at a wrong position. – Nebu Commented Jun 7, 2016 at 9:30
- Then use a better parser. For example, papaparse. deals with quoted values and line breaks in values just fine. Don't use regex for this. – Tomalak Commented Jun 7, 2016 at 11:33
2 Answers
Reset to default 13With 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.
Add conditions such as tab or start to the pattern as needed: (?:\t|^)"[^"]+"
\n(?=[^"]*"(?:[^"]*"[^"]*")*[^"]*$)
You can use this and replace by 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);