On the question of js regular expressions.

var str = "For more information, see Chapter 3.4.5.1";
var re = /see (chapter \d+(\.\d)*)/i;
var found = str.match(re);

console.log(found);

Why (\.\ d) finally matches .1 , shouldn"t it be .4.5.1 ? It doesn"t make sense.

Please correct me.

May.09,2022

The problem of

is related to the greedy pattern of regular expressions. (\.\ d) * is written in greedy mode by default. Its mechanism is that as long as a character satisfying the match is captured, it will continue to capture until the match is not satisfied, so when matching the character .4.5.1 , it will match .4 for the first time, .5 for the second time, and .1 for the third time.

I don't know if the subject asked in this place on MDN . In this example, there is a dot in the output explanation that breaks the problem, but it doesn't further explain the greed pattern.

// '.1' '(\.\d)'

1: "Chapter 3.4.5.1"

2: ".4.5.1"

3: ".1"
Menu