javascript - Regex JS. New line for each dot, but only outside of a quotation -
this strong me. give up, after 2 days, 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 wrap every dot, should not happen if dot inside quote
thanks
you can use lookahead based regex:
var re = /(?=(([^"]*"){2})*[^"]*$)\./g; var r; r = str.replace(/(?=(([^"]*"){2})*[^"]*$)\./g, '.\n'); quick "brown. fox". jumps over. "the lazy dog" r = str2.replace(re, '.\n'); quick «brown. fox». jumps over. «the lazy dog»
(?=(([^"]*"){2})*[^"]*$)
lookahead makes sure there number of quotes following dot making sure dot outside quotes. note quotes should balanced , unescaped.
Comments
Post a Comment