2

I cant find anywhere a working regex expression to find and replace the text between the div tags

So there is this html where i want to select everything between the <div class="info"> and </div> tag and replace it with some other texts

<div class="extraUserInfo">
        <p>Hello World! This is a sample text</p>
        <javascript>.......blah blah blah etc etc
</div>

and replace it with

My custom text with some codes
<tags> asdasd asdasdasdasdasd</tags>

so it would look like

<div class="extraUserInfo">
        My custom text with some codes
<tags> asdasd asdasdasdasdasd</tags>
</div>

here is a refiddle that all my code is there and as you can see I want to replace the whole bunch of codes between the and tag

http://refiddle.com/1h6j

Hope you get what I mean :)

EngrAbbas
  • 139
  • 1
  • 3
  • 10

1 Answers1

4

If there's no nesting, would just do a plain match non-greedy (lazy)

(?s)<div class="extraUserInfo">.*?</div>
  • .*? matches any amount of any character (as few as possible) to meet </div>
  • Used s modifier for making the dot match newlines too.

Edit: Here a Javascript-version without s modifier

/<div class="extraUserInfo">[\s\S]*?<\/div>/g

And replace with new content:

<div class="extraUserInfo">My custom...</div>

See example at regex101; Regex FAQ

Community
  • 1
  • 1
Jonny 5
  • 11,051
  • 2
  • 20
  • 42