What are ^.* and .*$ in regular expressions?

后端 未结 7 1095
臣服心动
臣服心动 2021-02-03 23:05

Can someone explain the meaning of these characters. I\'ve looked them up but I don\'t seem to get it.

The whole regular expression is:

/^.*(?=.{8,})(?=.         


        
相关标签:
7条回答
  • 2021-02-03 23:43

    Something else that my help you in the future:

    .*$
    

    will match two times given this string : "1"

    If you are wondering why, it's because it consumes all characters, but then also matches nothing. So the empty string is also a match.

    0 讨论(0)
  • 2021-02-03 23:45
    ^.* //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
    

    So when you see this...

    (?=.*[@#$%^&+=]).*$
    

    It allows any character (except line break) to come between (?=.*[@#$%^&+=]) and the end of the string.

    To show that . doesn't match any character, try this:

    /./.test('\n');  is false
    

    To actually match any character you need something more like [\s\S].

    /[\s\S]/.test('\n') is true
    
    0 讨论(0)
  • 2021-02-03 23:47

    Main docs: 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.
    
    0 讨论(0)
  • 2021-02-03 23:50

    This matches the beginning of the line (^) followed by any character (.*) :

    ^.*
    

    This matches the end of the line ($) preceded by any character (.*) :

    .*$
    
    0 讨论(0)
  • 2021-02-03 23:55
    • . means "any character".
    • * means "any number of this".
    • .* therefore means an arbitrary string of arbitrary length.
    • ^ indicates the beginning of the string.
    • $ indicates the end of the string.

    The regular expression says: There may be any number of characters between the expression (?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]) and the beginning and end of the string that is searched.

    0 讨论(0)
  • 2021-02-03 23:56

    That looks like a typical password validation regex, except it has couple of errors. First, the .* at the beginning doesn't belong there. If any of those lookaheads doesn't succeed at the beginning of the string, there's no point applying them again at the next position, or the next, etc..

    Second, while the regex insures that each of those three kinds of character is present, it doesn't say anything about the rest of the string. That may have been a deliberate choice, but people usually try to insure that only those kinds of characters are present. In that case, you would want to change the first lookahead from (?=.{8,}) to (?=[A-Za-z@#$%^&+=]{8,}$).

    End result:

    /^(?=[A-Za-z@#$%^&+=]{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$/
    
    0 讨论(0)
提交回复
热议问题