1

My regex (<(div|i)\b[^>]*>)(\bTEST\b)(<\/(div|i)\s*>)

replace = 'MOM' From

<div clas="dfsdf">TEST</div>

is

MOM

But I want the HTML tag persists It should look like this: <div clas="dfsdf">MOM</div>

What am I doing wrong?

Jens
  • 21
  • 3

2 Answers2

0

If you just replace with MOM, your whole match will be replaced by MOM..In your regex <div clas="dfsdf"> is group 1 ($1), TEST is capture group 3 ($3) and </div> is group 4 ($4)... So, you have to replace it with the following

$1MOM$4

to contain what is required and replace just the capture group corresponding to TEST.

karthik manchala
  • 13,492
  • 1
  • 31
  • 55
0

You could use look around:

preg_replace('~(?<=<(?:div|i)\b[^>]*>)TEST(?=</\s*(?:div|i)\s*>)~', "MOM', $str);
Toto
  • 89,455
  • 62
  • 89
  • 125