Nothing To Lose

If you don’t have it, how can you lose it!
Subscribe

Convert lines of text to HTML list item – using regex

March 21, 2011 By: Dexter Category: HTML, Regular Expressions, Web Development

Many a times I have to update pages where I need to display contents as HTML lists. The problems is the contents sent to me is in n number of lines of plain text.

Generally you will end up copy pasting <li> and </li> at the beginning and end of every line, for few lines it is ok, but what about if you have around 100 lines.

Well well well… if you knew regular expressions you life will be saved, here is what you do –

  • Copy paste all text in a text editor that supports find and replace with regular expression support.

This is line one
This is line two
This is line three
This is line four
This is line five
This is line ….
…………………….
…………………….
This is line fifty

  • open the replace dialogue box fill the following (make sure you have selected the regular-expression option for find)
    • find text : ^     (yup the carat symbol)
    • replace with : <li>
    • Hit replace all — all your lines should now be showing <li> in front of it.

<li>This is line one
<li>This is line two
<li>This is line three
<li>This is line four
<li>This is line five
<li>This is line ….
<li>…………………….
<li>…………………….
<li>This is line fifty

  • Open the replace dialogue box again and repeat the same steps again only:
    • find text now should be: $
    • replace with : </li>

<li>This is line one</li>
<li>This is line two</li>
<li>This is line three</li>
<li>This is line four</li>
<li>This is line five</li>
<li>This is line ….</li>
<li>…………………….</li>
<li>…………………….</li>
<li>This is line fifty</li>

  • Do forget to add unordered list or ordered list  tags <ul> or <ol> at the first line and </ul> or </ol> respectively as the last line to complete the HTML list.

<ul>

<li>This is line one</li>
<li>This is line two</li>
<li>This is line three</li>
<li>This is line four</li>
<li>This is line five</li>
<li>This is line ….</li>
<li>…………………….</li>
<li>…………………….</li>
<li>This is line fifty</li>

</ul>

And in three steps your complete text is now  a HTML list.

3 Comments to “Convert lines of text to HTML list item – using regex”


  1. Nice.

    How about
    $ cat filename |sed ‘s/^//g’|sed ‘s/$//g’

    1
  2. Your blog won’t allow li and /li within angular quote.
    But the idea remains same.

    2
  3. @anon: using sed is a nice idea indeed, I have used sed a lot and of course it solves the problem in one step also. But here I am not expecting people who are into web development to be using sed or are conversant with CLI usage.
    In fact the piping is also not required since you can run two scripts in sed in single go seperated by semicolon ;
    sed -e ’s/^/\/g;s/$/\< \/li\>/g’ < linesinfile.txt > lineswithlist.html

    have to use & gt ; to get the & lt ; to get the stuff.

    3

1 Trackbacks/Pingbacks

  1. HTML all you need to know» Blog Archive » Nothing To Lose | Convert lines of text to HTML list item – using … 21 03 11

Leave a Reply