views:

377

answers:

1

Anyone know of a good reg exp that can do this, optimally in one go? It needs to remove whitespace at begin/end of each line, remove LFs and CRs and replace with a single space but if there's a <br> (or <br/>) at the end of the line it should not add a space. I'd need this in a JavaScript conform regexp.

A: 

I would use something along these lines:

var str = '  foo<br>\nbar\nbaz \n quox\nquox';
// split into lines
var lines = str.split('\n');
// iterate over each line
for (var i = lines.length; i--; ) {
  // trim whitespace
  lines[i] = lines[i].replace(/^\s+|\s+$/g, '');
  // add whitespace at the end if string doesn't end with "<br>"
  if (!/<br>$/.test(lines[i])) lines[i] += ' ';
}
// concatenate into a string again
lines.join('');
kangax
Thanks kangax! That'll do! Though I've thought there might an ultimate regexp but this works fine too!
sascha
You'll be surprised but the bigger regex, the less efficient it usually is :) Often, it's better to break task into smaller chunks making life easier for yourself and for regex engine.
kangax