【发布时间】:2009-09-19 15:21:05
【问题描述】:
任何人都知道一个好的 reg exp 可以做到这一点,最好一次完成?它需要在每行的开头/结尾删除空格,删除 LF 和 CR 并替换为单个空格,但如果在行尾有 <br>(或 <br/>),则不应添加空格。我需要在符合 JavaScript 的正则表达式中使用它。
【问题讨论】:
标签: javascript xml actionscript-3 actionscript
任何人都知道一个好的 reg exp 可以做到这一点,最好一次完成?它需要在每行的开头/结尾删除空格,删除 LF 和 CR 并替换为单个空格,但如果在行尾有 <br>(或 <br/>),则不应添加空格。我需要在符合 JavaScript 的正则表达式中使用它。
【问题讨论】:
标签: javascript xml actionscript-3 actionscript
我会按照这些思路使用一些东西:
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('');
【讨论】: