0

I understand '?' makes a search lazy...and I feel that the following should work:

/<w:body>(.*)(<w:r>?)/igm

String to search:

<w:body><w:p w:rsidR="00584C40" w:rsidRDefault="00584C40" w:rsidP="00584C40"><w:pPr><w:rPr><w:b/></w:rPr></w:pPr><w:r><w:rPr><w:b/></w:rPr><w:t>This is going to be</w:t></w:r></w:p><w:p w:rsidR="00584C40" w:rsidRDefault="00584C40" w:rsidP="00584C40"><w:r><w:rPr><w:b/></w:rPr><w:t>More</w:t></w:r><w:r><w:t xml:space="preserve"> complicate </w:t></w:r><w:r><w:rPr><w:i/></w:rPr><w:t>text</w:t></w:r><w:r><w:t xml:space="preserve"> with</w:t></w:r></w:p><w:p w:rsidR="00584C40" w:rsidRDefault="00584C40" w:rsidP="00584C40"><w:pPr><w:pStyle w:val="ListParagraph"/><w:numPr><w:ilvl w:val="0"/><w:numId w:val="1"/></w:numPr></w:pPr><w:r><w:t>Bullets</w:t></w:r></w:p><w:p w:rsidR="00584C40" w:rsidRDefault="00584C40" w:rsidP="00584C40"><w:r><w:t>And</w:t></w:r></w:p><w:p w:rsidR="00584C40" w:rsidRDefault="00584C40" w:rsidP="00584C40"><w:pPr><w:pStyle w:val="ListParagraph"/><w:numPr><w:ilvl w:val="0"/><w:numId w:val="2"/></w:numPr></w:pPr><w:r><w:t>Numbered</w:t></w:r></w:p><w:p w:rsidR="00584C40" w:rsidRDefault="00584C40" w:rsidP="00584C40"><w:pPr><w:pStyle w:val="ListParagraph"/><w:numPr><w:ilvl w:val="0"/><w:numId w:val="2"/></w:numPr></w:pPr><w:r><w:t>Lists</w:t></w:r></w:p><w:p w:rsidR="00584C40" w:rsidRPr="00EA0565" w:rsidRDefault="00584C40" w:rsidP="00584C40"><w:r><w:t xml:space="preserve">With </w:t></w:r><w:r w:rsidRPr="002575C3"><w:rPr><w:sz w:val="36"/></w:rPr><w:t xml:space="preserve">BIG </w:t></w:r><w:r><w:t>fonts and weird charac</w:t></w:r><w:r w:rsidRPr="002575C3"><w:rPr><w:vertAlign w:val="superscript"/></w:rPr><w:t>ters</w:t></w:r><w:r><w:t>.</w:t></w:r></w:p><w:p w:rsidR="00000000" w:rsidRDefault="00584C40"/><w:sectPr w:rsidR="00000000" w:rsidSect="00B46591"><w:pgSz w:w="12240" w:h="15840"/><w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720" w:gutter="0"/><w:cols w:space="720"/><w:docGrid w:linePitch="360"/></w:sectPr></w:body></w:document>

Desired result:

<w:body><w:p w:rsidR="00584C40" w:rsidRDefault="00584C40" w:rsidP="00584C40"><w:pPr><w:rPr><w:b/></w:rPr></w:pPr><w:r>

Actual result (using https://regex101.com/r/uB1lQ8/1 and regexpal.com): (It still goes to the last vs. stopping at the first...)

<w:body><w:p w:rsidR="00584C40" w:rsidRDefault="00584C40" w:rsidP="00584C40"><w:pPr><w:rPr><w:b/></w:rPr></w:pPr><w:r><w:rPr><w:b/></w:rPr><w:t>This is going to be</w:t></w:r></w:p><w:p w:rsidR="00584C40" w:rsidRDefault="00584C40" w:rsidP="00584C40"><w:r><w:rPr><w:b/></w:rPr><w:t>More</w:t></w:r><w:r><w:t xml:space="preserve"> complicate </w:t></w:r><w:r><w:rPr><w:i/></w:rPr><w:t>text</w:t></w:r><w:r><w:t xml:space="preserve"> with</w:t></w:r></w:p><w:p w:rsidR="00584C40" w:rsidRDefault="00584C40" w:rsidP="00584C40"><w:pPr><w:pStyle w:val="ListParagraph"/><w:numPr><w:ilvl w:val="0"/><w:numId w:val="1"/></w:numPr></w:pPr><w:r><w:t>Bullets</w:t></w:r></w:p><w:p w:rsidR="00584C40" w:rsidRDefault="00584C40" w:rsidP="00584C40"><w:r><w:t>And</w:t></w:r></w:p><w:p w:rsidR="00584C40" w:rsidRDefault="00584C40" w:rsidP="00584C40"><w:pPr><w:pStyle w:val="ListParagraph"/><w:numPr><w:ilvl w:val="0"/><w:numId w:val="2"/></w:numPr></w:pPr><w:r><w:t>Numbered</w:t></w:r></w:p><w:p w:rsidR="00584C40" w:rsidRDefault="00584C40" w:rsidP="00584C40"><w:pPr><w:pStyle w:val="ListParagraph"/><w:numPr><w:ilvl w:val="0"/><w:numId w:val="2"/></w:numPr></w:pPr><w:r><w:t>Lists</w:t></w:r></w:p><w:p w:rsidR="00584C40" w:rsidRPr="00EA0565" w:rsidRDefault="00584C40" w:rsidP="00584C40"><w:r><w:t xml:space="preserve">With </w:t></w:r><w:r w:rsidRPr="002575C3"><w:rPr><w:sz w:val="36"/></w:rPr><w:t xml:space="preserve">BIG </w:t></w:r><w:r><w:t>fonts and weird charac</w:t></w:r><w:r w:rsidRPr="002575C3"><w:rPr><w:vertAlign w:val="superscript"/></w:rPr><w:t>ters</w:t></w:r><w:r>

Thank you for any expert help!

11teenth
  • 1,853
  • 1
  • 15
  • 28
  • *"I understand '?' makes a search lazy"* - Not used on its own, in which case it is equivalent to `{0,1}`. – nnnnnn Aug 03 '17 at 03:17
  • A `?` makes only the multiplyer lazy that is right in front of it. like `foo*?` or `foo+?` or `foo{1,1000}?`. The way you used it it means "optional" so that `>?` is equivalent to `>{0,1}` – Thomas Aug 03 '17 at 03:17

1 Answers1

0

Make the (.*) group lazy instead: /<w:body>(.*?)<w:r>/igm

const reg = /<w:body>(.*?)<w:r>/igm;
const str = `<w:body><w:p w:rsidR="00584C40" w:rsidRDefault="00584C40" w:rsidP="00584C40"><w:pPr><w:rPr><w:b/></w:rPr></w:pPr><w:r><w:rPr><w:b/></w:rPr><w:t>This is going to be</w:t></w:r></w:p><w:p w:rsidR="00584C40" w:rsidRDefault="00584C40" w:rsidP="00584C40"><w:r><w:rPr><w:b/></w:rPr><w:t>More</w:t></w:r><w:r><w:t xml:space="preserve"> complicate </w:t></w:r><w:r><w:rPr><w:i/></w:rPr><w:t>text</w:t></w:r><w:r><w:t xml:space="preserve"> with</w:t></w:r></w:p><w:p w:rsidR="00584C40" w:rsidRDefault="00584C40" w:rsidP="00584C40"><w:pPr><w:pStyle w:val="ListParagraph"/><w:numPr><w:ilvl w:val="0"/><w:numId w:val="1"/></w:numPr></w:pPr><w:r><w:t>Bullets</w:t></w:r></w:p><w:p w:rsidR="00584C40" w:rsidRDefault="00584C40" w:rsidP="00584C40"><w:r><w:t>And</w:t></w:r></w:p><w:p w:rsidR="00584C40" w:rsidRDefault="00584C40" w:rsidP="00584C40"><w:pPr><w:pStyle w:val="ListParagraph"/><w:numPr><w:ilvl w:val="0"/><w:numId w:val="2"/></w:numPr></w:pPr><w:r><w:t>Numbered</w:t></w:r></w:p><w:p w:rsidR="00584C40" w:rsidRDefault="00584C40" w:rsidP="00584C40"><w:pPr><w:pStyle w:val="ListParagraph"/><w:numPr><w:ilvl w:val="0"/><w:numId w:val="2"/></w:numPr></w:pPr><w:r><w:t>Lists</w:t></w:r></w:p><w:p w:rsidR="00584C40" w:rsidRPr="00EA0565" w:rsidRDefault="00584C40" w:rsidP="00584C40"><w:r><w:t xml:space="preserve">With </w:t></w:r><w:r w:rsidRPr="002575C3"><w:rPr><w:sz w:val="36"/></w:rPr><w:t xml:space="preserve">BIG </w:t></w:r><w:r><w:t>fonts and weird charac</w:t></w:r><w:r w:rsidRPr="002575C3"><w:rPr><w:vertAlign w:val="superscript"/></w:rPr><w:t>ters</w:t></w:r><w:r><w:t>.</w:t></w:r></w:p><w:p w:rsidR="00000000" w:rsidRDefault="00584C40"/><w:sectPr w:rsidR="00000000" w:rsidSect="00B46591"><w:pgSz w:w="12240" w:h="15840"/><w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720" w:gutter="0"/><w:cols w:space="720"/><w:docGrid w:linePitch="360"/></w:sectPr></w:body></w:document>`;

console.info(str.match(reg)[0]);
tklg
  • 2,572
  • 2
  • 19
  • 24