반응형

<html>

.

.

.

<div id="BCONTENT">

아무런 내용 http://www.test.com 링크 이동하기

</div>

.

,

,

</html>




<script type="text/javascript">

    var re = /(?:(?=(?:http|https):)([a-zA-Z][-+.a-zA-Z\d]*):(?:((?:[-_.!~*'()a-zA-Z\d;?:@&=+$,]|%[a-fA-F\d]{2})(?:[-_.!~*'()a-zA-Z\d;\/?:@&=+$,\[\]]|%[a-fA-F\d]{2})*)|(?:(?:\/\/(?:(?:(?:((?:[-_.!~*'()a-zA-Z\d;:&=+$,]|%[a-fA-F\d]{2})*)@)?(?:((?:(?:(?:[a-zA-Z\d](?:[-a-zA-Z\d]*[a-zA-Z\d])?)\.)*(?:[a-zA-Z](?:[-a-zA-Z\d]*[a-zA-Z\d])?)\.?|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|\[(?:(?:[a-fA-F\d]{1,4}:)*(?:[a-fA-F\d]{1,4}|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})|(?:(?:[a-fA-F\d]{1,4}:)*[a-fA-F\d]{1,4})?::(?:(?:[a-fA-F\d]{1,4}:)*(?:[a-fA-F\d]{1,4}|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}))?)\]))(?::(\d*))?))?|((?:[-_.!~*'()a-zA-Z\d$,;:@&=+]|%[a-fA-F\d]{2})+))|(?!\/\/))(\/(?:[-_.!~*'()a-zA-Z\d:@&=+$,]|%[a-fA-F\d]{2})*(?:;(?:[-_.!~*'()a-zA-Z\d:@&=+$,]|%[a-fA-F\d]{2})*)*(?:\/(?:[-_.!~*'()a-zA-Z\d:@&=+$,]|%[a-fA-F\d]{2})*(?:;(?:[-_.!~*'()a-zA-Z\d:@&=+$,]|%[a-fA-F\d]{2})*)*)*)?)(?:\?((?:[-_.!~*'()a-zA-Z\d;\/?:@&=+$,\[\]]|%[a-fA-F\d]{2})*))?)(?:\#((?:[-_.!~*'()a-zA-Z\d;\/?:@&=+$,\[\]]|%[a-fA-F\d]{2})*))?)/img;


    function makeHTML(textNode) {

        var source = textNode.data;

        return source.replace(re, function() {

            var url = arguments[0];

            var a = $('<a></a>').attr({ 'href': url, 'target': '_blank' }).text(url);

            return url.match(/^https?:\/\/$/) ? url : $('<div></div>').append(a).html();

        });

    };


    function eachText(node, callback) {

        $.each(node.childNodes, function() {

            if (this.nodeType != 8 && this.nodeName != 'A') {

                this.nodeType != 1 ? callback(this) : eachText(this, callback);

            }

        });

    };


    function autolink(obj) {

        return obj.each(function() {

            var queue = [];

            eachText(this, function(e) {

                var html = makeHTML(e);

                if (html != e.data) {

                    queue.push([e, makeHTML(e)]);

                }

            });

            $.each(queue, function(i, x) {

                $(x[0]).replaceWith(x[1]);

            });

        });

    };



   $(document).ready(function() {

      autolink($("#BCONTENT")); 

    });

</script>

반응형
Posted by Hippalus
,