正则表达式中的^.*和.*$是什么?

log*_*tef 15 regex

有人可以解释这些人物的意义.我看了他们,但我似乎没有得到它.

整个正则表达式是:

/^.*(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$/
Run Code Online (Sandbox Code Playgroud)

所以基本上是正则表达式和结束字符的开头.

Til*_*lge 35

  • . 意思是"任何角色".
  • * 意思是"任何数量的这个".
  • .* 因此意味着任意长度的任意字符串.
  • ^ 表示字符串的开头.
  • $ 表示字符串的结尾.

正则表达式表示:表达式(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=])与搜索字符串的开头和结尾之间可能有任意数量的字符.


小智 14

^.* //Start of string followed by zero or more of any character (except line break)

.*$ //Zero or more of any character (except line break) followed by end of string
Run Code Online (Sandbox Code Playgroud)

所以当你看到这个......

(?=.*[@#$%^&+=]).*$
Run Code Online (Sandbox Code Playgroud)

它允许任何字符(除了换行符)介于(?=.*[@#$%^&+=])字符串之间和字符串的结尾.

要显示.与任何字符都不匹配,请尝试以下操作:

/./.test('\n');  is false
Run Code Online (Sandbox Code Playgroud)

要真正匹配任何角色你需要更像的东西[\s\S].

/[\s\S]/.test('\n') is true
Run Code Online (Sandbox Code Playgroud)


Mar*_*c B 6

主要文档:http://www.php.net/manual/en/reference.pcre.pattern.syntax.php

/^.*(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$/
12345   6         7                                  89

1 - start of pattern, can be almost any character, and must have a matching character at the END of the pattern (see #9 below)
2 - anchors the pattern to the start of a line of text
3 - `.` matches any character
4 - a modifier, "0 or more of whatever came before"
  - `.*` means "0 or more of ANY characters"
5 - A positive lookahead assertion: http://www.php.net/manual/en/regexp.reference.assertions.php
6 - A repetition indictor: http://www.php.net/manual/en/regexp.reference.repetition.php
  - `{8,}` = "at least 8 of whatever came previously"
  - `.{8,}` = "at least 8 'any' characters"
7 - A character class: http://www.php.net/manual/en/regexp.reference.character-classes.php
  - `[a-z]` - any one character in the range 'a' - 'z' (the lower case alphabet)
8 - anchors the pattern to the end of the line
9 - end of the pattern, must match character used in #1 above.
Run Code Online (Sandbox Code Playgroud)


AsT*_*TeR 5

这匹配行首 (^) 后跟任何字符 (.*) :

^.*
Run Code Online (Sandbox Code Playgroud)

这匹配以任何字符 (.*) 开头的行 ($) 的结尾:

.*$
Run Code Online (Sandbox Code Playgroud)