A problem with the lazy pattern of Javascript regular expressions?

The

regular expression lazy pattern means that if the expression matches successfully, it will match as few characters as possible.
according to the literal meaning, take a look at my code as follows

var pattern=/a(\w*?)/;  
var str="a123a";  
console.log(str.replace(pattern,"$1"));

I expect the output is" 1century, because of the lazy mode, matching a character can make it a success
, but the actual output is" 123a", which is the same as the greedy mode output

.

Why is this?


* means matching 0 to multiple times , so matches at least 0 characters. Because it is lazy matching , matching a empty string , that is, / a (\ code?) / and / a / are equivalent;
$1 means
therefore, str.replace (pattern,'$1') is equivalent to str.replace (/ a code,') , that is, replace the first a in the string with the empty string ; Note that / a / is not a global match, so only the first a is replaced; if you want to replace all a , you need to set pattern to global match pattern = / a (\ pattern?) / g ;


$1 matches the first (), that is, the matching content in the group

w means matching all strings followed by means zero or any number of times? For example, do can match do or doo. Since an is followed by a value, it all matches

.
Menu