-1

Actually i have string in java having the some html content now i want to replace with some other html content by using regular expressions in java

String htmlData="<textarea style="display:none"/> some other data here <textarea/> some more other data here <input type="text"/>";

Now i want to replace like the following :

    String htmlData="<textarea style="display:none"></textarea> some other data here <textarea></textarea> some more other data here <input type="text"/>";

means that i need to change all self close textarea tags to right syntax textarea tags.

Here is what I tried:

 String htmlData = htmlData.replace("<textarea/>","<textarea></textarea>");

But how can I find textarea nodes that have attributes ?

Stephan
  • 41,764
  • 65
  • 238
  • 329
Naresh Kallamadi
  • 163
  • 1
  • 14
  • 1
    Your question doesn't explain what you've done to try and solve the problem; it currently reads like a request for code. Please share your attempted implementation and explain how it fails to meet your requirements. – Duncan Jones Jan 03 '14 at 10:41
  • I got enlightened a few days back. Your turn! http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – Haywire Jan 03 '14 at 10:53

3 Answers3

1

I don't know why this has anything to do with a "second occurrence", but this should work:

htmlData = htmlData.replaceAll("(<textarea[^/]*)/>", "$1></textarea>");

Some test code:

String htmlData = "<textarea style=\"display:none\"/> some other data here <textarea/> some more other data here <input type=\"text\"/>";
htmlData = htmlData.replaceAll("(<textarea[^/]*)/>", "$1></textarea>");
System.out.println(htmlData);

Output:

<textarea style="display:none"></textarea> some other data here <textarea></textarea> some more other data here <input type="text"/>
Bohemian
  • 412,405
  • 93
  • 575
  • 722
1

This one should work or you:

htmlData = htmlData.replaceAll("(<textarea[^>]*)/>", "$1></textarea>");
Sabuj Hassan
  • 38,281
  • 14
  • 75
  • 85
0

Try this:

<textarea[^/]*/>    
Stephan
  • 41,764
  • 65
  • 238
  • 329