Oscar Zarrus
Oscar Zarrus

Reputation: 790

Regex JS. New line for each dot, but only outside of a quotation

This is very strong for me. I give up, after two days, I ask you

Turn this

var str = 'The quick "brown. fox". jumps over. "the lazy dog"'
var str2 = 'The quick «brown. fox». jumps over. «the lazy dog»'

Into this

The quick "brown. fox".
jumps over.
"the lazy dog"

or

The quick «brown. fox». 
jumps over. 
«the lazy dog»

In other words I would like to wrap every dot, but this should not happen if the dot is inside a quote.

Upvotes: 1

Views: 209

Answers (3)

YOU
YOU

Reputation: 123881

With match

str.match(/((?:"[^"]*"|«[^»]+»|[^".«]+)+(?:\.|$))\s*/g).join("\n")

"The quick "brown. fox". 
jumps over. 
"the lazy dog""

str2.match(/((?:"[^"]*"|«[^»]+»|[^".«]+)+(?:\.|$))\s*/g).join("\n")

"The quick «brown. fox». 
jumps over. 
«the lazy dog»"

Upvotes: 1

Wolfgang Kluge
Wolfgang Kluge

Reputation: 895

Another aproach (like in JavaScript : Find (and Replace) text that is NOT in a specific HTML element?), this could be solved by matching both, the quoted string and the desired period.

In the replace-function you then have the chance to change only the single periods...

txt.replace(/("[^"]*"|«[^»]*»)|(\.)/g, function (_, quoted, dot) {
    if (quoted) return quoted;
    return '.\n';
});

Upvotes: 1

anubhava
anubhava

Reputation: 785651

You can use this lookahead based regex:

var re = /(?=(([^"]*"){2})*[^"]*$)\./g;
var r;

r = str.replace(/(?=(([^"]*"){2})*[^"]*$)\./g, '.\n');
 The quick "brown. fox".
 jumps over.
 "the lazy dog"

r = str2.replace(re, '.\n');
 The quick «brown.
 fox».
 jumps over.
 «the lazy dog»

(?=(([^"]*"){2})*[^"]*$) is a lookahead that makes sure there are even number of quotes following dot thus making sure dot is outside the quotes. However note that quotes should be balanced and unescaped.

Upvotes: 3

Related Questions