Regex matches only once
html, php, regex
Solution
Use `+?` instead of `+` for non-greedy match.
\[img\](.+?)\[/img\]
Demo https://www.regex101.com/r/zW9zJ0/1
`+` Will match one of more occurrence. Combined with `?`, it will match as least as possible (lazy).
Problem
I have a regex that should rewrite `[img]foo.bar[/img]` to `<img src=foo.bar>` and in fact, it does. The problem comes in when I have two or more of those `[img]url.ext[/img]` in the string. Instead of matching each one separately, it matches the first `[img]` and the last `[/img]` and leaves the urls and tags in the middle as part of the `src`. My php code is `$newstring = preg_replace('%\[img\](.+)\[/img\]%', '<img src=${1}>', $string);` a working example is https://www.regex101.com/r/mJ9sM0/1