HTML is too complex to be parsed with regular expressions, so you won't be able to do this unless there are certain assumptions you can make about the code you're looking for that will allow it to be found with a regular expression.
Because <p>
tags can't be nested inside one another, you should be able to guarantee that everything between your opening tag and the next instance of <p>
will be what you want to remove, even if it contains nested HTML tags inside like that <a>
tag.
That is, of course, assuming this HTML code isn't invalid in that particular way. And also assuming that there aren't any HTML comments in here that might contain that text.
By default, regular expressions are greedy. That means any time you have something like .*
it will try to match as many characters as it possibly can. This can be a problem when you're trying to match content between an opening pattern and a closing pattern, if that pattern might appear multiple times. However, you can use ?
to make a regular expression not greedy.
Try using a regular expression like this to match those HTML tags you're looking for:
/<p style="flex: 0 0 auto; margin:0; padding-right:1rem;">.*?<\/p>/g