简体   繁体   English

为什么字符类和捕获组在javascript正则表达式中对于空格字符后跟点会显示不同的结果?

[英]Why character class and capturing group show different results in javascript regexp for a whitespace character followed by a dot?

I was solving the chapter exercises from this book - http://eloquentjavascript.net/09_regexp.html 我正在解决本书中的章节练习-http://eloquentjavascript.net/09_regexp.html

There is a question where I need to write a regular expression for a whitespace character followed by a dot, comma, colon, or semicolon. 有一个问题,我需要为空格字符编写正则表达式,后跟点,逗号,冒号或分号。

I wrote this one 我写了这个

var re1 = /\s(.|,|:|;)/;

The book had this as answer 这本书有答案

var re2 = /\s[.,;:]/;

I understand that the second one is correct, and it is more efficient. 我知道第二个是正确的,而且效率更高。 But leaving behind efficiency, the first one should also give correct results. 但是不考虑效率,第一个也应该给出正确的结果。

The first one doesn't give correct output for the following piece of code - 对于下面的代码,第一个没有给出正确的输出-

console.log(re1.test("escape the dot"));  // prints true

It should have given "false" but it outputs the opposite. 它应该给出“ false”,但输出相反。 I couldn't understand this. 我听不懂 I tried https://www.debuggex.com/ too, but the figure also seems to be okay! 我也尝试过https://www.debuggex.com/ ,但这个数字似乎还可以!

It seems that I am missing some understanding from my end. 看来我末日缺少一些了解。

Just as I finished this question to post, I realised my mistake that was giving me the wrong output. 当我完成要发布的问题时,我意识到我的错误给了我错误的输出。 So, I thought I would rather share both the question and answer here so as to help anyone who might face some similar problem in future. 因此,我认为我宁愿在这里分享问题和答案,以帮助将来可能会遇到类似问题的任何人。

The thing is the period (dot) itself, when used between square brackets, loses its special meaning. 问题是句点(点)本身在方括号之间使用时会失去其特殊含义。 The same goes for other special characters, such as +. 其他特殊字符(例如+)也是如此。

But they retain their special meaning when used in a capturing group. 但是在捕获组中使用时,它们保留了其特殊含义。

So, the code 所以,代码

var re1 = /\s(.|,|:|;)/;
console.log(re1.test("escape the dot"));  // prints true

is rather looking for the pattern - a space followed by either a character that's not newline ( because of period ), or any of comma, colon, and semi-colon. 而是在寻找模式-一个空格,后跟一个不是换行符的字符(由于period),或者逗号,冒号和分号中的任何一个。

To get the correct output, the correct re, if used with capturing group, would be, 为了获得正确的输出,如果与捕获组一起使用,则正确的re将是:

var re1 = /\s(\.|,|:|;)/;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM