繁体   English   中英

来自 freeCodeCamp 的“正负前瞻”问题

[英]Questions from the freeCodeCamp "Positive and Negative Lookahead"

我正在考虑来自 freeCodeCamp 的这个问题。 这是链接: 正则表达式:正负前瞻

挑战:在 pwRegex 中使用前瞻来匹配长度超过 5 个字符、不以数字开头且具有两个连续数字的密码。

 let sampleWord = "astronaut"; let pwRegex = // Change this line result = pwRegex.test(sampleWord);

首先,该网站显示的 model 答案是; /^\D(?=\w{5})(?=\w*\d{2})/;

这是我的问题;

  1. 为什么 5 后不需要逗号?
    问题是“长度超过 5 个字符”,所以我认为应该是 {5,},而不是 {5}。

  2. 如何提前检查连续数字?
    我认为“(?= \ w * \ d {2})”部分不足以检查连续数字。 我在我的 chrome 控制台上尝试了以下代码,

     let mytest ="pass75"; let Regex = /^\D(?=\w{5,})(?=\w*\d{2})/; //This is the site's model answer. Regex.test(mytest);   //=>True

    “7”和“5”不是连续的数字,但它说的是真的,所以我认为这个 model 答案不完整。 你怎么看? 有没有人可以解释如何解决这个问题?

我也被卡住了,解决这个问题。 然而,前瞻被认为是棘手的。 我们可以分解问题中提到的约束,然后将它们中的每一个结合起来以完全解决它。

好吧,现在,你的问题:

  1. 当您说“至少多个”时,逗号是必需的,即,如果我需要考虑至少 7 个字符的模式,它应该是{7,} 而且,是的,你是绝对正确的,但是。 挑战没有放置测试用例来检查那个用例。 他们可能更关心内部模式而不是字符数。

  2. 是的,对于连续的数字, \d{2}是不够的。 这些数字应该是连续的,但不仅是连续的,这意味着我们还必须考虑在所需的 2 个连续数字之前(但不是开头)或之后可能有“零个或多个”数字的情况。 因此,您可以考虑为此添加一个模式: \d*

因此,您可以提出与此类似的解决方案来解决挑战: /(?=\w{6,})(?=^[^0-9]+\d*\D*\d{2})/

提醒一下,我对这个任务的理解不是匹配两个连续的相同数字的字符,即“22”或“33”,而只是匹配两个连续的数字,即“23”或“49”。 因此, \d{2}就可以了。

我自己被困在这个练习上一段时间了!

正则表达式可以写成 /(?=\w{6,})(?=\w*\d{2})/ 提到的两个条件的两个括号

 Challenge: Use lookaheads in the pwRegex to match passwords that are greater than 5 characters long, do not begin with numbers, and have two consecutive digits. let sampleWord = "astronaut"; let pwRegex = // Change this line result = pwRegex.test(sampleWord); // solution let sampleWord = "astronaut"; let pwRegex = /(?=\w{5,})(?=\D\w*\d{2})/; /* we put {5,} not 5. because in question they asked characters greater than 5,*/ let result = pwRegex.test(sampleWord);

 /*Fix the regex so that it checks for the names of Franklin Roosevelt or Eleanor Roosevelt in a case sensitive manner and it should make concessions for middle names. Then fix the code so that the regex that you have created is checked against myString and either true or false is returned depending on whether the regex matches.*/ let myString = "Eleanor Roosevelt"; let myRegex = /False/; // Change this line let result = false; // Change this line // After passing the challenge experiment with myString and see how the grouping works // solution => let myString = "Eleanor Roosevelt"; let myRegex = /(Franklin ([AZ]\. )?|Eleanor ([AZ]\. )?)Roosevelt/; let result = false; resutl = myRegex.test(myString); //=> this should work

我就是这样解决的! 记住长度大于(不 >=) 5 个字符的匹配密码。 Rest 在代码中非常自我解释。

let sampleWord = "astronaut";
let pwRegex = /(?=\w{6,})(?=.*\d{2,}.*)/; // Change this line
let result = pwRegex.test(sampleWord);

首先,对 freeCodeCamp 中的问题稍作修改。

在 pwRegex 中使用前瞻来匹配长度大于 5 个字符且具有两个连续数字的密码。

 let sampleWord = "astronaut"; let pwRegex = /change/; // Change this line let result = pwRegex.test(sampleWord);

我的回答是针对这个问题:

/(?=\w{5,})(?=\D+\d{2})/

如您所见,我在 5 之后使用了逗号。这个答案通过了所有测试。

答案1:

但是,您找到的答案在第二部分中使用了\w *。 这意味着字母数字可以匹配零个或多个字符。 因此,这提供了“长度超过 5 个字符”的条件。

答案 2:

在这个问题中,连续的意思是“一个接一个”,例如38, 11, 40, 23而不是124, 3456

暂无
暂无

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

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