-2

I have HTML code in my Sublime Text, and I want to replace my code in my Sublime Text with Find and Replace in Sublime Text, anyone can help me?

I have code like this

</div>
<li>lorem</li>
<li>ipsum</li>
<div>

I want to replace

</div>
<li>

to

</div>
<ul>
<li>

and

</li>
<div>

to

</li>
</ul>
<div>

But I want to replace at the same time. How can I do this? Replacing the many different strings in line at one time

Rohman HM
  • 2,539
  • 3
  • 25
  • 40
  • [Obligatory](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags). The take-home message - ***Don't use regex to process HTML. Use an HTML parser.*** – MattDMo Sep 24 '16 at 13:11
  • Hello MattDMo, I ask how change it in Sublime Text, not in HTML – Rohman HM Sep 27 '16 at 06:38
  • I never said anything about doing it in HTML. This task *may* be possible in Sublime using multiple search and replace steps, but that is not the point. The point is that you should be using an HTML **parser** for doing this, **not** regex. If you know Python, use [`beautifulsoup4`](https://www.crummy.com/software/BeautifulSoup/). For Ruby, try [Nokogiri](http://www.nokogiri.org/). For Java, try [`jsoup`](https://jsoup.org/). Use [`HTML::Parser`](http://search.cpan.org/dist/HTML-Parser/Parser.pm) for Perl. Any other languages, just Google. – MattDMo Sep 27 '16 at 11:45
  • Ok. Thanks MattDMo, I will try – Rohman HM Sep 27 '16 at 12:20

1 Answers1

0

Try this. First you need to turn the code into a string, then use a simple replace:

var code = '</div><li>lorem</li><li>ipsum</li><div></div><li>lorem</li><li>ipsum</li><div></div><li>lorem</li><li>ipsum</li><div></div><li>lorem</li><li>ipsum</li><div>';

var newCode = code.replace(/<\/div>\s*?<li>/g, '</div><ul><li>').replace(/<\/li>\s*?<div>/g, '</li></ul><div>');

document.getElementById('output').innerHTML = newCode;
<div id = "output"></div>
sideroxylon
  • 4,338
  • 1
  • 22
  • 40