Alexey Ozerov
Alexey Ozerov

Reputation: 1542

RegEx replace text only within specific tag

I'm trying to replace Foo by Bar only within <body> tag in HTML.

https://regex101.com/r/RVgSZ1/1

Replace regex (\<body[\s\S]*?)Foo([\s\S]*?\<\/body\>) by $1Bar$2 only replacing the first occurrence of foo.

How to replace them all?

The solution like (?:<body|\G)[^<]*?\KFoo(?=[^<]*<\/body>) suggested at Find and replace all occurrences of a string inside an HTML tag in one pass doesn't work either as it assumes there are no nested tags.

Upvotes: -7

Views: 39

Answers (0)

Related Questions