Javascript-how to replace the text in the editor as a link in real time?

Source: Internet
Author: User
For example, if a website is pasted in the Zhihu editor, it is automatically converted to a link. The code found on the stack does not know how to use it. let's take a look. {Code...} is it replaced by monitoring keyup? Hope you can answer this question! Thank you! * ** Update: 2013-12-09: ***... for example, in the Zhihu editor, if you paste a website, Is automatically converted to a link.
The code found on the stack does not know how to use it. let's take a look.

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

Is it replaced by monitoring keyup? Hope you can answer this question! Thank you!

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

The editor is

Reply content:

For example, if a website is pasted in the Zhihu editor,Is automatically converted to a link.
The code found on the stack does not know how to use it. let's take a look.

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

Is it replaced by monitoring keyup? Hope you can answer this question! Thank you!

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

The editor is

Not recommendedkeyupEvent, because it does not respond to non-keyboard operations, such as pasting the mouse into a Web site. UseonPropertyChangeEventtextareaChanges in the attribute values of the instance are generated.

In addition, we recommend that you add a space at the end of the regular expression, that isURL + spaceIt is resolved as a link. if it is a previous match, it will lead to an endless loop.

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

  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(/ 0) {            var combinedReplacedText;            //Keep delimiter when splitting            var splitInput = originalText.split(/(<\/a>)/g);            for (i = 0; i < splitInput.length; i++) {                if (splitInput[i].match(/$1').replace(replacePattern2, '$1$2').replace(replacePattern3, '$1');                }            }            combinedReplacedText = splitInput.join('');            return combinedReplacedText;        } else {            replacedText = inputText.replace(replacePattern1, '$1');            replacedText = replacedText.replace(replacePattern2, '$1$2');            replacedText = replacedText.replace(replacePattern3, '$1');            return replacedText;        }    },

For a piece of text, it can be run repeatedly.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.