-1

I have a JS regex to replace sections of a string that include img tags inside p tags. I'm picking up ones I don't want.

Sample of what I want to find and replace (the src and class are not always in the same order and imageData is not always the same base64 "numbers", so I can't just match on the whole string):


<p><img class="classIWantToMatch" src="data:image/gif;base64,dataForImage"></p>
<p><img src="data:image/gif;base64,imageData" class="classIWantToMatch" ></p>

Sample that is also getting found and replaced (but I don't want to):

<p><img src="/myFiles/my.PNG" alt=""></p>

Regex:

/(<p><img).*?(class="classIWantToMatch").*?(<\/p>)/g

I'm trying to figure out lookahead and lookbehind, but I can't seem to make it do what I want.

Besides ye olde basic googling, I tried here:

A Regex that search for lines that contains a string, and not contains another string

Using Regex to find string that contains one string and excludes a second string?

and several other ones as well. I think I just don't understand lookahead/behind.

Jeff
  • 1
  • 1

1 Answers1

0

This RegEx might help you to only get your desired target or design an expression to do so:

(<p><img.+class=\")(classIWantToMatch)(\".+<\/p>) 

enter image description here

  • You can only bound your desired target in second capturing group.
  • With a simple left boundary as (<p><img.+class=\") and right boundary as (\".+<\/p>) you might be able to do so. However, you might consider making these boundaries more restrictive just to be safe. For instance, .+ can be much limited to a list of chars.
Emma
  • 27,428
  • 11
  • 44
  • 69
  • 1
    I'm trying this out in regex101, but it barrels right past the closing p tag and ends up encompassing subsequent p tags. How can I stop it at the first p tag that follows (at some varying interval) the classIWantToMatch? – Jeff May 03 '19 at 17:01
  • 1

    Some text

    • Entrances increased 6% from 5,681 to 6,009
      • Some text

      Some Text

      Some Text

    – Jeff May 03 '19 at 17:12
  • The above is a sample of code that is "failing" in regex101, it goes all the way to the last closing p tag it can find. – Jeff May 03 '19 at 17:13
  • 1
    The p tag (containing the img tag) that I need to replace will always have that class, mce-pagebreak. I suppose it could have others, but it WILL have that one. – Jeff May 03 '19 at 17:20
  • ok, thank you very much for helping! – Jeff May 03 '19 at 17:38