Hopefully a simple question.
If I have text like this:
Orange
Apples
Melons
Bananas
...how can I replace all occurrences of multiple blank lines with single blank lines, ending up with this:
Orange
Apples
Melons
Bananas
Fiddle: http://jsfiddle.net/Jq4pT/ Need to remove multiple blank lines from the first box's input before inserting into the second.
I've found this link, but am not sure how to use that regular expression in Javascript?
Thanks.
This is an old question, but I don't care, it comes up first in Google search.
You can do this with the following code:
var EOL = string.match(/\r\n/gm)?"\r\n":"\n";
var regExp = new RegExp("("+EOL+"){3,}", "gm");
string = string.replace(regExp, EOL+EOL);
g modifier to replace all occurrencesm modifier to apply regex to the whole multi-line string, instead of per line.Just use it like this:
var inputString = '...';
var outputString = inputString.replace(/^(\s*\r\n){2,}/, "\r\n")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With