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

后端 未结 7 1185
臣服心动
臣服心动 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: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
    

提交回复
热议问题