It doesn't work because .*
is greedy and will try to take every character it possibly can in the match. In this case, it'll match everything up until the last ...< /a>. and then match the last >
Try this: <a.*?>
.*?
is the lazy version of .*
which will only match the minimum number of characters needed to make the match successful. In this case, the first >
it encounters, right before the contents of the a
tag.
Additionally, if you want to also remove the </a>
then you should try this instead: <\/?a.*?>
\/
will match < / a> and the ?
means it's optional to have so it will still match the first <a>
tag. .*?
won't affect the second match since .*?
can match 0 characters (it is lazy after all!)
Regex101