简体   繁体   中英

What does this `/^.*$/` regex match?

I'm maintaining some old code when I reached a headscratcher. I am confused by this regex pattern: /^.*$/ (as supplied as an argument in textFieldValidation(this,'true',/^.*$/,'','' ).

I interpret this regex as:

  • /^=open pattern
  • .=match a single character of any value (Except EOL)
  • *=match 0 or more times
  • $=match end of the line
  • /=close pattern

So…I think this pattern matches everything, which means the function does nothing but waste processing cycles. Am I correct?

^ "Starting at the beginning."
. "Matching anything..."
* "0 or more times"
$ "To the end of the line."

Yep, you're right on, that matches empty or something.

And a handy little cheat sheet.

It matches a single line of text.

It will fail to match a multiline String, because ^ matches the begining of input, and $ matches the end of input. If there are any new line ( \\n ) or caret return ( \\r ) symbols in between - it fails.

For example, 'foo'.match(/^.*$/) returns foo .

But 'foo\\nfoo'.match(/^.*$/) returns null .

The regexp checks that the string doesn't contain any \\n or \\r . Dots do not match new-lines.

Examples:

/^.*$/.test("");  // => true
/^.*$/.test("aoeu");  // => true
/^.*$/.test("aoeu\n");  // => false
/^.*$/.test("\n");  // => false
/^.*$/.test("aoeu\nfoo");  // => false
/^.*$/.test("\nfoo");  // => false

Yes, you are quite correct. This regex matches any string that not contains EOL (if dotall=false) or any string (if dotall=true)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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