I want make javascript HTML converter to Text. All done I works good, but I can't process links. I need the reg expression which is make in text version link as

Html version:

<a href="http://link.com">Link text</a>

convert link to Text version:

Link text(http://link.com)

My code

$('body').on('click','[data-action="convertTemplateToText"]', function() {
    var html = $("#clientHTML").val();

    if (html) {
        html = html.replace(/<!doctype.*>/i,"");
        html = html.replace(/<head>[\w\W]*?<\/head>/i,"");
        html = html.replace(/<style.*>[\w\W]*?<\/style>/gi,"");
        html = html.replace(/<script.*>[\w\W]*?<\/script>/gi,"");
        html = html.replace(/\n|\r/g," ");
        html = html.replace(/\<\/p\>/gi,"\n");
        html = html.replace(/\<\/li\>/gi," ");
        html = html.replace(/\<br\s*?\/?\>/gi,"\n");
        html = strip_tags(html,'<a>');
        html = html_entity_decode(html,'HTML_ENTITIES');
        html = html.replace(/([ \t])+/g," ");
        html = html.replace(/\n /g,"\n");

        if (html.charAt(0) == ' ') {
            html = html.substr(1);
        }
    } else {
        html = '';
    }

    $("#clientText").val(html);
    $('#templateTextContainer').slideDown(500);

    return false;
});

Help me please

有帮助吗?

解决方案 2

I don't know if you're using jQuery or not, but with it it's pretty simple:

$('a').each(function() {
    var $text = $(this).html();
    var $link = $(this).attr('href');
    $(this).after($text+" ("+$link+")");
    $(this).remove();
});

EDIT3 (corrected error from comment):

OK, I've achieved what you need:

/<\s*a.*?href\s*=\s*(?:"|')(.*?)(?:"|')[^>]*>(.*?)<\s*?\/\s*?a\s*?>/ig

And the substitution would be:

$2 ($1)

Here's a working example: http://regexr.com/38qgv

I've also added a check to include malformed tags such as < a href = ""> or < / a >

其他提示

You can use TextVersionJS which is an open source lib that solves the very same problem, you have. It does not depend on any other libs, and you can use it in the browser and in node.js as well.

    <!DOCTYPE html>
     <body>
      <div id='tempDiv'></div>
        <script>
          var html
          html='<h3><a href="//stackoverflow.com">current community</a></h3>'
          alert(toText(html))

The following function converts to text any html content passed to it

            function toText(content) {
             document.getElementById('tempDiv').innerHTML = content
             return document.getElementById('tempDiv').textContent
            }
        </script>

     </body>
    </html>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top