简体   繁体   中英

Regex - Right/Left trim + reduce more than one space in middle to one + remove line break at beginning & end incl reduction to two if in middle & >2

I need to right/left trim a string, reduce the multiple spaces in the middle to one ... but exclude line breaks. That's what I tried:

var trimMessageTest = varMessageTest.replace(/^\s+|\s+$/g,'');      // Trim whitespace left & right
var newMessageTest = trimMessageTest.replace(/(?:(?!\n\r)\s\s+)/g,' ');  // Reduce whitespace and exclude line break
//var newMessageTest = trimMessageTest.replace(/\s\s+/g,' ');       // Reduce whitespace    
console.log(newMessageTest);

And as a last step remove line break at beginning and end ... and reduce multiple line break if > 2 ... to two line breaks like this:

var lastMessageTest = newMessageTest.replace(/((?:\r\n?|\n)+)$|(?:\r\n?|\n){2,}/g, function ($0,$1) {
  return $1 ? '' : '\n\n';
});
console.log(newMessageTest);

What's the best way to meet this requirement?

I would use a simple function :

function cleanupText(text) {
  return text && text            // Ensure text exists
    .trim()                      // Trim left and right spaces
    .replace(/\n{2,}/g, '\n\n')  // Replace 2+ linebreaks with 2 ones
    .replace(/ +/g, ' ');        // Replace consecutive spaces with one
}

Snippet

Use ECMAScript 6 (ES6) compatible browser (not IE) to run snippet as code use template literals delimited by backticks:

 "use strict"; function cleanupText(text) { return text && text // Ensure text exists .trim() // Trim left and right spaces .replace(/\\n{2,}/g, '\\n\\n') // Replace 2+ linebreaks with 2 ones .replace(/ +/g, ' '); // Replace consecutive spaces with one } const sampleText = ` Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. `; // Cleanup text console.log(cleanupText(sampleText)); 

Here is one of possible solutions

 var str = document.getElementById("ta").value; function cleanupText(text) { ///^(?:\\s)+|[\\s+]$/g var str = ""; text // Trim whitespace left < start string .replace(/^\\s+/, "") // get each line .match(/[^\\r\\n|^\\r|^\\n]+/g).filter(function(string){ str += string // Trim whitespace left & right < each line .trim() // Replace consecutive spaces with one .replace(/\\s+/g, " ") // replace with single tab > to count end breakline + "\\t"; }); // only last tabs > count end breakline var two = str.match(/\\t+$/g)[0].match(/\\t/g); // and yea! return str.replace(/\\t+/g, "\\n") + (two.length>1?"\\n\\n":""); } console.log(cleanupText(str)); 
 <textarea id="ta" style="width:300px;height: 150px"> sdgfghfd gdfghdf fdghdf sdgfghfd gdfghdf fdghf </textarea> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM