When I learn the lesson " Find Characters with Lazy Matching "
I wonder there may be a requirement “match repeat elements”, like I wrote below:
xmlInfo:
<any-other-marks>
<parameter key="onepeice" value="1" />
<treasures>
<parameter key="theotherpeice" value="2" type="0"/>
<any-other-marks>
I use /<parameter.*?\/>.*?<parameter.*?\/>/
to match the two special parameters one time
now I tried /(<parameter.*?\/>).*?\1/
, and the result is different
so how should I reduce this repeat part?
ill take another look. but first things first. this, seems like no good syntax to me:
.*?
.* means: 0 or more.
? means, 0 or 1
in otherwords
.* overlaps ?
in the challenge you link to, no repeat is needed.
the solution is:
let myRegex = /<h1>/;
but you prob know this already.
can you give better example of what strings you wanna match this on?
QQstone
September 2, 2019, 2:57am
3
sorry for late reply
according to my understanding, .*? means match any string in lazy mode. In this mode, matching stops at the first time when found the sub-string which meets the rules of the regExp context
Then I should re-define xmlInfo as
<any-other-marks>
<parameter key="onepeice" value="1" />
<treasures>
<parameter key="theotherpeice" value="2" type="0"/>
<any-other-marks>
<parameter key="a" value="1" />
<parameter key="b" value="1" />
<parameter key="c" value="1" />
<parameter key="d" value="1" />
so using/<parameter.*?\/>.*?<parameter.*?\/>/
should found this part
<parameter key="onepeice" value="1" />
<treasures>
<parameter key="theotherpeice" value="2" type="0"/>
otherwise I got almost the whole xmlInfo if I delete the character “?” in the regExp
the issue is how to reduce the repeated part in /<parameter.*?\/>.*?<parameter.*?\/>/