-1

I'm trying to write a regex that matches any <img /> tag followed with a space, followed with some text.

In other words, I want these lines to be matched:

  • Hello <img /> world!
  • <span>Hello <img /></span> world!
  • <span>Hello <img /> </span>world!

But not those ones:

  • Hello <img />!
  • <span>Hello <img /></span>!
  • <span>Hello </span> world!
  • <span>Hello <img /></span>my world!

While I managed to match images using <img[\w\W]+?\/>, I can't figure out how to match the space followed with text, especially when some closing tags are located in between...

See RegExr here.

Gyum Fox
  • 3,287
  • 2
  • 41
  • 71

2 Answers2

1

Update #1

New update matches following tags as well like in <span>Hello <img /></span> world!

If you need to match whole line you need to see whether each line matches corresponding regex or not then try to output it using:

^(.*?<img[^>]*>(<\/?\w+[^>]*>)* +\S).*

Note: watch m flag.

Live demo

Otherwise if you are trying to match tags following a space following a non-space this is how it could be done in regex:

<img[^>]*>(<\/?\w+[^>]*>)* +(?=\S)
                             ^ constructing a positive lookahead

Live demo

revo
  • 47,783
  • 14
  • 74
  • 117
  • The last regex does not check for the img tag. It matches the span. Let me add an extra example to exclude to my question – Gyum Fox Apr 27 '17 at 14:11
  • It doesn't match `img` only since you said it this way: *any tag followed with a space,* – revo Apr 27 '17 at 14:12
  • oh, my mistake. The title of the question was correct though. Sorry, i'll update... – Gyum Fox Apr 27 '17 at 14:13
  • Then you only need to replace `\w+` with `img\b`. – revo Apr 27 '17 at 14:13
  • I've updated the question... Sorry I typed in the question but It got evaluated as HTML... – Gyum Fox Apr 27 '17 at 14:14
  • Actually, I add to change the regex to `]*>(?><\/?\w+[^>]*>)* +(?=\S)` or else it would lead to a catastrophic backtracking failure, as can be experienced here: https://regex101.com/r/WSGRW8/1 – Gyum Fox Jan 22 '19 at 13:13
1

Would /<img.+?\/>(?=.* )/gi work?

http://regexr.com/3frgd

Edit

With the newer test case, here is a version for image tags immediately followed by a space with other tags optionally before it

/<img.+?\/>(?=(<.*?>)* )/gi

http://regexr.com/3frgm

Community
  • 1
  • 1
Joseph Marikle
  • 76,418
  • 17
  • 112
  • 129
  • Not really since it matches `Hello my world!` too. – revo Apr 27 '17 at 14:20
  • @revo, but that would be a string with an `` followed by a space. I thought that's what we were going for? – Joseph Marikle Apr 27 '17 at 14:21
  • Where do you see following space then? – revo Apr 27 '17 at 14:21
  • @revo between "my" and "world". You seem to believe OP means "immediately followed by" and I seem to believe OP means just "followed by". We might have to get input from OP to know for sure. – Joseph Marikle Apr 27 '17 at 14:23
  • Well then don't you think OP would have re-phrased requirements in some other ways like *any `` tags followed by anything up to a space*? – revo Apr 27 '17 at 14:27
  • @JosephMarikle I did not mean to match `Hello my world!` --just updated the question to make it clear – Gyum Fox Apr 27 '17 at 14:31
  • @revo I never presume an original poster is going to phrase things the best way possible. This is a site that uses a forum format for a reason. Even if this answer is not directly applicable to OP, it's another way to potentially solve what was asked. That makes it potentially helpful to someone in the future who needs to match "an image tag followed by a space". Multiple approaches to a problem isn't a bad thing. – Joseph Marikle Apr 27 '17 at 14:34
  • Caring of future needy people also worth it. That’s like saying fat people are healthy because they *might* exercise. – revo Apr 27 '17 at 14:39
  • @revo I'm sorry, but I'm missing the analogy. That comparison doesn't make any sense to me. – Joseph Marikle Apr 27 '17 at 14:43
  • More of an irony than a comparison. Never mind, future people may find it understandable. – revo Apr 27 '17 at 14:46
  • @revo No, what's ironic is the lack of correct sentence structure, grammar, and spelling in your comment; which is the real problem regarding its readability. Regardless, I don't think you quite understand how this site works. Like I said before, multiple answers is a good thing, and not something to be discouraged. Why don't you instead suggest how this answer can be improved? – Joseph Marikle Apr 27 '17 at 14:59
  • Thank you for telling me how to talk, in memory of our English class professor: *lack of correct sentence structure...* (which returns one and half page of Google search results). I actually was going to offer an improvement over your answer by trying to give you to understand your misinterpretation of OP's need but you started to show resistance. So I'm not the culprit. – revo Apr 27 '17 at 15:18