简体   繁体   English

JavaScript:如何使用正则表达式从字符串中删除空行?

[英]JavaScript: how to use a regular expression to remove blank lines from a string?

I need to use JavaScript to remove blank lines in a HTML text box.我需要使用 JavaScript 删除 HTML 文本框中的空行。 The blank lines can be at anywhere in the textarea element.空行可以位于textarea元素中的任何位置。 A blank line can be just a return or white spaces plus return.空行可以只是一个回车或空格加回车。

I am expecting a regular expression solution to this.我期待正则表达式解决方案。 Here are some I tried, but they are not working and cannot figure out why:以下是我尝试过的一些方法,但它们不起作用,无法弄清楚原因:

/^\s*\r?\n/g   

/^\s*\r?\n$/g

Edit 1编辑 1

It appears that the solution (I modified it a little) suggested by aaronman and m.buettner works:看来 aaronman 和 m.buettner 建议的解决方案(我稍微修改了一下)有效:

string.replace(/^\s*\n/gm, "") 

Can someone tell why my first regular expression is not working?有人能说出为什么我的第一个正则表达式不起作用吗?

Edit 2编辑 2

After reading all useful answers, I came up with this:在阅读了所有有用的答案后,我想出了这个:

/^[\s\t]*(\r\n|\n|\r)/gm

Is this going to be one that cover all situations?这将是一种涵盖所有情况的方法吗?

Edit 3编辑 3

This is the most concise one covering all spaces (white spaces, tabs) and platforms (Linux, Windows, Mac).这是涵盖所有空间(空格、制表符)和平台(Linux、Windows、Mac)的最简洁的一种。

/^\s*[\r\n]/gm

Many thanks to m.buettner!非常感谢 m.buettner!

Your pattern seems alright, you just need to include the multiline modifier m , so that ^ and $ match line beginnings and endings as well:您的模式似乎没问题,您只需要包含多行修饰符m ,以便^$匹配行的开头和结尾:

/^\s*\n/gm

Without the m , the anchors only match string-beginnings and endings.没有m ,锚只匹配字符串的开头和结尾。

Note that you miss out on UNIX-style line endings (only \\r ).请注意,您错过了 UNIX 样式的行尾(仅\\r )。 This would help in that case:在这种情况下,这将有所帮助:

/^\s*[\r\n]/gm

Also note that (in both cases) you don't need to match the optional \\r in front of the \\n explicitly, because that is taken care of by \\s* .另请注意,(在这两种情况下)您不需要显式匹配\\n前面的可选\\r ,因为这是由\\s*

As Dex pointed out in a comment, this will fail to clear the last line if it consists only of spaces (and there is no newline after it).正如Dex在评论中指出的那样,如果最后一行仅包含空格(并且后面没有换行符),则它无法清除最后一行。 A way to fix that would be to make the actual newline optional but include an end-of-line anchor before it.解决这个问题的一种方法是使实际的换行符可选,但在它之前包含一个行尾锚点。 In this case you do have to match the line ending properly though:在这种情况下,必须符合正确的结束,虽然该行:

/^\s*$(?:\r\n?|\n)/gm

我相信这会奏效

searchText.replace(/(^[ \t]*\n)/gm, "")

This should do the trick i think:这应该可以解决我认为的问题:

var el = document.getElementsByName("nameOfTextBox")[0];
el.value.replace(/(\r\n|\n|\r)/gm, "");

EDIT: Removes three types of line breaks.编辑:删除三种类型的换行符。

Here's another solution :这是另一个解决方案

string = string.replace(/^(?=\n)$|^\s*|\s*$|\n\n+/gm, "")

It appears to handle both blank lines and space-only lines.它似乎处理空行和仅空格行。

function removeEmptyLine(text) {
  return text.replace(/(\r?\n)\s*\1+/g, '$1');
}

test:测试:

console.assert(removeEmptyLine('a\r\nb') === 'a\r\nb');
console.assert(removeEmptyLine('a\r\n\r\nb') === 'a\r\nb');
console.assert(removeEmptyLine('a\r\n \r\nb') === 'a\r\nb');
console.assert(removeEmptyLine('a\r\n \r\n  \r\nb') === 'a\r\nb');
console.assert(removeEmptyLine('a\r\n \r\n 2\r\n  \r\nb') === 'a\r\n 2\r\nb');
console.assert(removeEmptyLine('a\nb') === 'a\nb');
console.assert(removeEmptyLine('a\n\nb') === 'a\nb');
console.assert(removeEmptyLine('a\n \nb') === 'a\nb');
console.assert(removeEmptyLine('a\n \n  \nb') === 'a\nb');
console.assert(removeEmptyLine('a\n \n2 \n  \nb') === 'a\n2 \nb');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript正则表达式,用于删除字符串末尾的字符 - Javascript regular expression to remove characters from the end of a string Javascript正则表达式忽略空行/双倍间距 - Javascript regular expression to ignore blank lines/double spacing 如何在Wordpress,PHP,JavaScript上使用正则表达式从字符串(URL)中删除特殊字符 - How to remove special characters from a string (URL) using regular expression on wordpress, php, javascript 如何使用正则表达式或纯 JavaScript 从字符串中删除所有括号及其内容并保留其他文本格式 - How to remove all parenthesis and their contents from a string using a regular expression or pure JavaScript and retain other text format 如何使用正则表达式删除Javascript中的文本? - How do I use a regular expression to remove text in Javascript? 如何在javascript中使用正则表达式 - how to use regular expression in javascript 如何在JavaScript中使用正则表达式用空字符串替换一组字符? - How to use Regular expression in JavaScript to replace a set of characters with an empty string? 如何在带有url的正则表达式字符串的javascript中查找? - How to find in javascript with regular expression string from url? 如何使用javascript正则表达式从字符串中获取域 - How to get domain from a string using javascript regular expression 如何将字符串从PHP转换为javascript正则表达式? - How to convert string from PHP to javascript regular expression?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM