正则表达式匹配 Javascript 中的回车
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13527186/
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
Regex to match carriage return in Javascript
提问by Andy Kaufman
I am trying to write some Javascript to hide some elements that contain only carriage returns. I appreciate that the correct way to solve this problem would be to stop these elements being created, but unfortunately that is not possible in this instance. I am trying to user a regular expression to search for the unwanted elements but am not having much luck. The function I have written is as follows:
我正在尝试编写一些 Javascript 来隐藏一些仅包含回车符的元素。我明白解决这个问题的正确方法是停止创建这些元素,但不幸的是,在这种情况下这是不可能的。我正在尝试使用正则表达式来搜索不需要的元素,但运气不佳。我写的函数如下:
function HideEmptyP()
{
var patt = (\r)
for(var i = 0;i<desc[i].length;i++);
{
var desc[i] = document.getElementsByClassName('sitspagedesc');
var result[i] = patt.test(desc[i]);
if (result[i] == true)
{
desc[i].style.display='none';
}
else
{
alert("No Match!");
}
}
The error I'm getting in the Web Console is 'Syntax Error: Illegal Character'.
我在 Web 控制台中遇到的错误是“语法错误:非法字符”。
Grateful for any ideas on how to solve this.
感谢您提供有关如何解决此问题的任何想法。
Thanks in advance.
提前致谢。
回答by T.J. Crowder
I am trying to write some Javascript to hide some elements that contain only carriage returns.
我正在尝试编写一些 Javascript 来隐藏一些仅包含回车符的元素。
There's no need for a regular expression for that, just compare the element's innerHTML
property to "\\r"
, e.g.:
不需要正则表达式,只需将元素的innerHTML
属性与进行比较"\\r"
,例如:
if (demo[i].innerHTML === "\r") {
// Remove it
}
But beware that some browsers may transform a single carriage return. You might want to check for "\\r"
, "\\n"
, and just a space. To do that, you mightwant to use a regular expression.
但请注意,某些浏览器可能会转换单个回车符。您可能要检查"\\r"
,"\\n"
和只是一个空间。为此,您可能需要使用正则表达式。
Your regular expression literal ((\\r)
) is just completely invalid, it's worth reading up on themto learn the correct syntax. To write a regular expression literal in JavaScript, you use /
as the delimiter. So: /\\r/
. To test that a string contains only \r
, \n
, or space, you can use /^[\r\n ]+$/
(which requires there be at least one character that matches, and uses ^
to indicate start-of-string, and $
to indicate end-of-string):
您的正则表达式文字 ( (\\r)
) 完全无效,值得阅读它们以了解正确的语法。要在 JavaScript 中编写正则表达式文字,请/
用作分隔符。所以:/\\r/
。为了测试该字符串只包含\r
,\n
或空间,可以使用/^[\r\n ]+$/
(这需要有至少一个字符匹配,并采用^
以表明启动的字符串,并$
指示结束的字符串):
if (demo[i].innerHTML.match(/^[\r\n ]+$/) {
// Remove it
}
回答by RomainValeri
Your litteral seems odd.
你的文字看起来很奇怪。
Try var patt = /\r/;
尝试 var patt = /\r/;
回答by Techmonk
The reason you are getting Syntax error is because the declaration var patt = (\r) is incorrect it should be somethign like var patt = '\r';
您收到语法错误的原因是声明 var patt = (\r) 不正确,它应该类似于 var patt = '\r';
Also the whole for loop is wrong.
整个for循环也是错误的。
You should define demo before you start the for loop not inside it, and result need not be an array but just a normal variable
您应该在开始不在其中的 for 循环之前定义 demo,并且 result 不必是数组而只是一个普通变量
回答by Avinash Barnwal
var patt=/\n/gi
should work.
应该管用。
extra i flag to denote case insensitive. g for global search.
额外的 i 标志表示不区分大小写。g 用于全局搜索。