在JavaScript正则表达式中链接多个正向前瞻 [英] Chaining multiple positive lookaheads in JavaScript regex

查看:98
本文介绍了在JavaScript正则表达式中链接多个正向前瞻的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是学习正则表达式的新手,我遇到了这个答案使用正向前瞻来验证密码。

I'm new to learning Regular Expressions, and I came across this answer which uses positive lookahead to validate passwords.

正则表达式是 - (/^(?=.*\d)(?=.*[az])(?=.*[AZ])[0-9a-zA-Z]{8,}$/)以及用户提供的细分是 -

The regular expression is - (/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{8,}$/) and the breakdown provided by the user is -

(/^
(?=.*\d)                //should contain at least one digit
(?=.*[a-z])             //should contain at least one lower case
(?=.*[A-Z])             //should contain at least one upper case
[a-zA-Z0-9]{8,}         //should contain at least 8 from the mentioned characters
$/)

但是,我不太清楚将多个前瞻链接在一起。根据我所学到的,正向前瞻检查表达式是否后跟前瞻中指定的内容。例如,这个答案说 -

However, I'm not very clear on chaining multiple lookaheads together. From what I have learned, a positive lookahead checks if the expression is followed by what is specified in the lookahead. As an example, this answer says -


正则表达式是(?= all)匹配字母,但前提是它们是立即跟随的通过字母所有

The regex is(?= all) matches the letters is, but only if they are immediately followed by the letters all

所以,我的问题是个人的前瞻性如何工作?如果我将其分解 -

So, my question is how do the individual lookaheads work? If I break it down -


  1. 第一部分是 ^(?=。* \ d)。这是否表示在字符串的开始处,查找任何字符的零次或多次出现,后跟1位数(从而检查是否存在1位数)?

  2. 如果第一部分是正确的,那么第二部分(?=。* [az]),是否检查之后检查字符串开头的第1步,查找任何字符的零次或多次,后跟小写字母?或者两个先行者是否彼此完全无关?

  3. 此外,每个前瞻周围的()的用途是什么?它是否创建了捕获组?

  1. The first part is ^(?=.*\d). Does this indicate that at the starting of the string, look for zero or more occurrences of any character, followed by 1 digit (thereby checking the presence of 1 digit)?
  2. If the first part is correct, then with the second part (?=.*[a-z]), does it check that after checking for Step 1 at the start of the string, look for zero or more occurrences of any character, followed by a lowercase letter? Or are the two lookaheads completely unrelated to each other?
  3. Also, what is the use of the ( ) around every lookahead? Does it create a capturing group?

我还看了 Rexegg文章关于前瞻,但它没有多大帮助。

I have also looked at the Rexegg article on lookaheads, but it didn't help much.

非常感谢任何帮助。

推荐答案

正如评论中提到的,这里的关键点不是前瞻,而是回溯
(?=。* \ d)查找完整的行(。* ),然后回溯以找到至少一个数字( \d )。



这在不同的前瞻中重复出现,可以像这样进行优化:

As mentionned in the comments, the key point here are not the lookaheads but backtracking: (?=.*\d) looks for a complete line (.*), then backtracks to find at least one number (\d).


This is repeated throughout the different lookaheads and could be optimized like so:

(/^
(?=\D*\d)                // should contain at least one digit
(?=[^a-z]*[a-z])         // should contain at least one lower case
(?=[^A-Z]*[A-Z])         // should contain at least one upper case
[a-zA-Z0-9]{8,}          // should contain at least 8 from the mentioned characters
$/)

此处,对比原则适用。

这篇关于在JavaScript正则表达式中链接多个正向前瞻的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆