0

I have a list of lines containing text links in a HTML file that I want to transform into hyperlinks and li lists in JavaScript.

Example :

<ul>
   links
   links text
   links
</ul>

to

<ul>
   <li><a href='links'>links</a></li>
   <li><a href='links'>links</a> text</li>
   <li><a href='links'>links</a></li>
</ul>

This script transforms text links into hyperlinks : http://jsfiddle.net/RH8f6/94

I tried to do the same for adding li tags to each lines:

//Wrap each line with li tags
       $('#links').ready(function() {
        // Get each ul
        $('ul').each(function(){
            // Get the content
            var str = $(this).html();
            // Select each lines starting with url
            //trim blank space before and include text after the links)
            var regex = /(http.*?).*/ig
            // Wrap each new line in textarea with li tags
            var replaced_text = str.replace(regex, "<li>$1</li>");
            // Echo link
            $(this).html(replaced_text);
        });
    });

... and it doesn't work. I wish to do the multiple replacement preferably in one single JavaScript function.

Thanks a lot in advance!

Sam
  • 1
  • 1
    isn't that your demo turns the plain texts into links OK? – King King May 13 '14 at 09:38
  • You are using invalid HTML markup, you shouldn't expect it to work as 'expected'. BTW, you shouldn't use any regex to parse HTML http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – A. Wolff May 13 '14 at 09:41
  • @KingKing Yes the demo turns the plain text links into hyperlinks and it works – Sam May 13 '14 at 11:24

1 Answers1

0

I'd rather do something like this:

$('ul>a').wrap("<li/>");
Maurice Perry
  • 32,610
  • 9
  • 70
  • 97