Assignment (. +?) In regular expressions

I am new to this thing.

When analyzing code, I often come across a template .+?or(.+?)

I can not find the meaning of this template using my deductive reasoning noobish.

+3
source share
2 answers

.means any character (except a new line). +means one or more. ?in this context mean lazy or inanimate. This means that he will try to match the absolute minimum of characters that satisfy the quantifier. Example:

> 'abc'.match(/.+/)
["abc"]
> 'abc'.match(/.+?/)
["a"]
> 'abc'.match(/.*/)
["abc"]
> 'abc'.match(/.*?/)
[""]
+13
source

, . , , , :

  • .
  • + 1
  • .+
  • ? ungreedy, , .

:

/.+X/.exec("aaaXaaaXaaa");
["aaaXaaaX"]
/.+?X/.exec("aaaXaaaXaaa");
["aaaX"]

, ? , , .

+5

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


All Articles