Javascript RegExp:匹配除换行符以外的任何内容 (\r?\n)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4344819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 11:06:59  来源:igfitidea点击:

Javascript RegExp: Match anything but newlines (\r?\n)

javascriptregex

提问by Jiew Meng

I want to have my RegExp match anything but a newline

我想让我的 RegExp 匹配除换行符以外的任何内容

\r?\n

采纳答案by Gumbo

This should do it:

这应该这样做:

/(?:[^\r\n]|\r(?!\n))/g

This matches either any character except \rand \nor a single \rthat is not followed by \n.

这匹配除\rand之外的任何字符\n\r后面没有\n.

回答by Breezer

you can use the negative delimiter !

您可以使用负分隔符!

so (?!(\r|\n))

所以 (?!(\r|\n))

try that

试试看

or this maybe

或者这也许

.+?(?!(\r|\n))

回答by Tom Gullen

As an alternative suggestion why not avoid regexp?

作为替代建议,为什么不避免使用正则表达式?

var newText = oldText.replace("\r","").replace("\n","");

This will return the string removing all the instances of \rand \n

这将返回字符串中除去的所有实例\r\n