-1

Is it possible to replace all matches between two sub strings in one regular expression?

My case is that I want to inject HTML after the li tag but only inside myList.

Before:

<ul class="myList">
    <li>List item</li>
    <li>List item</li>
</ul>

After (... would be the injected markup):

<ul class="myList">
    <li>...List item</li>
    <li>...List item</li>
</ul>

Any help would be great, thanks.

RichardG
  • 445
  • 3
  • 8
  • 17

1 Answers1

1

My case is that I want to inject HTML after the li tag but only inside myList.

Other than for trivial cases where it's easy to write a "quick, hacky" script to get the job done, you should never use regular expressions to parse HTML.

A "quick, hacky" script in this case could be e.g. to search for:

<ul class="myList">.*<li>([^<]*)</li>(?=.*</ul>)

(Note: this probably needs to be a multi-line search; if there's no option for this then replace .* with [\s\S]*.)

...And then replace the value of the first match group (probably represented by $1 or \1, depending on how you do this).

However, as per my link above, I'd like to emphasise that this is not a perfect answer. It is literally impossible to perfectly parse HTML with a regular expression.

To do this "properly", you must use an XML parser instead.

Community
  • 1
  • 1
Tom Lord
  • 27,404
  • 4
  • 50
  • 77
  • To give you an idea of just *some* of the flaws with my answer: `1.` What if there are more spaces in the HTML tags? `2.` What if there are other classes, IDs, style attributes, data attributes, etc in the HTML tags? `3.` What if there are multiple `
      ` elements on the page? `4.` What if the `
    • ` element contains a `<` character, e.g. a ``? ........
    – Tom Lord May 09 '16 at 16:08