javascript - 如何实时替换编辑器内的文本成为链接?

WBOY
Release: 2016-06-06 20:48:11
Original
1190 people have browsed it

例如知乎编辑器内,如果粘贴了一段网址,会自动转换为链接。
stack 上找到的代码不知道该如何用,大家可以看看。

<code>function replaceURLWithHTMLLinks(text) {
    var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
    return text.replace(exp,"<a href="%241">$1</a>"); 
   }
</code>
Copy after login
Copy after login

是监视 keyup 来替换的吗?希望得到大家的解答!谢谢!

*** // Update:2013-12-09 :***

编辑器为 <div contenteditable="true"></div>

回复内容:

例如知乎编辑器内,如果粘贴了一段网址,会自动转换为链接。
stack 上找到的代码不知道该如何用,大家可以看看。

<code>function replaceURLWithHTMLLinks(text) {
    var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
    return text.replace(exp,"<a href="%241">$1</a>"); 
   }
</code>
Copy after login
Copy after login

是监视 keyup 来替换的吗?希望得到大家的解答!谢谢!

*** // Update:2013-12-09 :***

编辑器为 <div contenteditable="true"></div>

不推荐用keyup事件,因为它对非键盘操作的动作没反应,比如鼠标粘贴进来一个网址。这时候需要用onPropertyChange事件,能够对textarea内的属性值的变化产生动作。

另外,正则部分推荐最后加一个空格判定,也就是URL+空格才解析为链接形式,如果是之前的匹配的话会造成死循环。

http://jsfiddle.net/5kmqe/1/

<code class="lang-javascript">  linkify: function(inputText) {
        var replacedText, replacePattern1, replacePattern2, replacePattern3;
        var originalText = inputText;
        //URLs starting with http://, https://, file:// or ftp://
        replacePattern1 = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gi;
        //URLs starting with "www." (without // before it, or it'd re-link the ones done above).
        replacePattern2 = /(^|[^\/f])(www\.[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gi;
        //Change email addresses to mailto:: links.
        replacePattern3 = /(([a-zA-Z0-9\-\_\.])+@[a-zA-Z\_]+?(\.[a-zA-Z]{2,6})+)/gi;
        //If there are hrefs in the original text, let's split
        // the text up and only work on the parts that don't have urls yet.
        var count = originalText.match(/<a href if> 0) {
            var combinedReplacedText;
            //Keep delimiter when splitting
            var splitInput = originalText.split(/()/g);

            for (i = 0; i $1</a>').replace(replacePattern2, '$1<a href="http://%242" target="_blank">$2</a>').replace(replacePattern3, '<a href="mailto:%241">$1</a>');
                }
            }
            combinedReplacedText = splitInput.join('');
            return combinedReplacedText;
        } else {
            replacedText = inputText.replace(replacePattern1, '<a href="%241" target="_blank">$1</a>');
            replacedText = replacedText.replace(replacePattern2, '$1<a href="http://%242" target="_blank">$2</a>');
            replacedText = replacedText.replace(replacePattern3, '<a href="mailto:%241">$1</a>');
            return replacedText;
        }
    },
</code>
Copy after login

对一段文本,可重复运行。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!