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

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -