简体   繁体   English

这个`/^.*$/`正则表达式匹配什么?

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

I'm maintaining some old code when I reached a headscratcher. 当我到达一个headscratcher时,我正在维护一些旧代码。 I am confused by this regex pattern: /^.*$/ (as supplied as an argument in textFieldValidation(this,'true',/^.*$/,'','' ). 我对此正则表达式模式感到困惑: /^.*$/ (作为textFieldValidation(this,'true',/^.*$/,'',''的参数提供textFieldValidation(this,'true',/^.*$/,'','' )。

I interpret this regex as: 我把这个正则表达式解释为:

  • /^=open pattern / ^ =打开模式
  • .=match a single character of any value (Except EOL) 。=匹配任何值的单个字符(EOL除外)
  • *=match 0 or more times * =匹配0次或更多次
  • $=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" * “0次或更多次”
$ "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. 它将无法匹配多行String,因为^匹配输入的开头, $匹配输入的结尾。 If there are any new line ( \\n ) or caret return ( \\r ) symbols in between - it fails. 如果中间有任何新行( \\n )或插入符号返回( \\r )符号 - 则失败。

For example, 'foo'.match(/^.*$/) returns foo . 例如, 'foo'.match(/^.*$/)返回foo

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

The regexp checks that the string doesn't contain any \\n or \\r . regexp检查字符串是否包含任何\\n\\r \\n 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) 此正则表达式匹配任何不包含EOL(如果dotall = false)或任何字符串(如果dotall = true)的字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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