开发者

regular expression harming <br /> , embedded objects and iframes

i found this great code to linkify plain text to links...

function text_to_link(inputText)
            {
                /*var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
                return text.replace(exp,"<a href='$1' target='_blank'>$1</a>");*/

                var replaceText, replacePattern1, replacePattern2, replacePattern3;

                //URLs starting with http://, https://, or ftp://
                replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
                var replacedText = inputText.replace(replacePattern1, '<a href="$1" target="_blank">$1</a>');

                //URLs starting with www. (without // before it, or it'd re-link the ones done above)
                replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
                replacedText = replacedText.replace(replacePattern2, '$1<a href="http://$2" target="_blank">$2</a>');

                //Change email addresses to mailto:: links
                replacePattern3 = /(\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6})/gim;
                repla开发者_StackOverflowcedText = replacedText.replace(replacePattern3, '<a href="mailto:$1">$1</a>');

                return replacedText;
    }

now the code is great but it is harming the embedded objects and iframes ...

 <object width="300" height="182"><param name="movie" value="http://www.youtube.com/v/xVaBIF1LnwY?fs=1&amp;hl=en_GB"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/xVaBIF1LnwY?fs=1&amp;hl=en_GB" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="560" height="340"></embed></object>

It also eats up all the br / (line breaks) in the text...

The sample page here

can we just code it to leave all the text that is inside "object and object" or "iframe and iframe"

Thanks


There is an standard solution to this:

  1. First "encode" occurances of inputText inside object and iframe tags so that http|ftp|www|@. This way these sequences are not replaced in step 2.

  2. Do the replacement as in the original function.

  3. Now "decode" the occurances of http|ftp|www|@ back.

Jakub


i could somehow use this... but not a good solution...

     function text_to_link(inputText)
            {

                var object_text = new Array();
                var oi = 0;

                while ( inputText.indexOf('<object') >= 0)
                {
                    var si = inputText.indexOf('<object');
                    var ei = inputText.indexOf('</object>');
                    object_text[oi] = inputText.substring(si,ei+9);

                    inputText = inputText.replace(object_text[oi] , '[ob_service]');
                    oi ++;
                }

                var iframe_text = new Array();
                var ii = 0;
                while ( inputText.indexOf('<iframe') >= 0)
                {
                    var si = inputText.indexOf('<iframe');
                    var ei = inputText.indexOf('</iframe>');
                    iframe_text[ii] = inputText.substring(si,ei+9);

                    inputText = inputText.replace(iframe_text[ii] , '[if_service]');
                    ii ++;
                }

                var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
                inputText = inputText.replace(exp,"<a href='$1' target='_blank'>$1</a>");



                oi =0;
                while (inputText.indexOf('[ob_service]') >= 0)
                {
                    inputText = inputText.replace('[ob_service]' , object_text[oi]);
                    oi ++;
                }

                ii =0;
                while (inputText.indexOf('[if_service]') >= 0)
                {
                    inputText = inputText.replace('[if_service]' , iframe_text[ii]);
                    ii ++;
                }


                return inputText;

            }

Please anyone knowing regular expressions do something good...

Thanks

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜