What matches this `` /^.*$/` regular expression?

I support some old code when I get to the boss. I am confused by this regex pattern: /^.*$/ (as indicated in the argument in textFieldValidation(this,'true',/^.*$/,'','' ).

I interpret this regex as:

  • / ^ = open pattern
  • . = matches one character of any value (except EOL)
  • * = match 0 or more times
  • $ = end of line end
  • / = close template

So ... I think this template matches everything, which means that the function does nothing except waste processing cycles. I'm right?

+6
source share
4 answers

It corresponds to one line of text.

It will not correspond to a multi-line String, because ^ corresponds to the beginning of input, and $ corresponds to the end of input. If between them there is a new line ( \n ) or a carriage return character ( \r ) - it fails.

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

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

+5
source

^ "Starting from the beginning."
. "Matching anything ..."
* "0 or more times"
$ "End of line".

Yes, you are right, it corresponds to empty or something.

And a convenient little cheat sheet.

+5
source

The regular expression checks that the string does not contain \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 
+5
source

Yes, you are absolutly right. This regex matches any string that does not contain EOL (if dotall = false) or any string (if dotall = true)

+1
source

Source: https://habr.com/ru/post/944655/


All Articles