JavaScript-How do I replace text in the editor in real time as a link?

Source: Internet
Author: User
For example, in the editor, if you paste a Web site, are automatically converted to links.
The code found on the stack doesn't know how to use it, so you can look at it.

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

Is it a surveillance keyup to replace it? Hope to get everyone's answer! Thank you!

update:2013-12-09:* * *

Editor is

Reply content:

For example, in the editor, if a URL is pasted, it is automatically converted to a link.
The code found on the stack doesn't know how to use it, so you can look at it.

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

Is it a surveillance keyup to replace it? Hope to get everyone's answer! Thank you!

update:2013-12-09:* * *

Editor is

Event is not recommended keyup because it does not respond to non-keyboard actions, such as the mouse pasted in a URL. In this case, you need to use onPropertyChange events to be able to textarea create actions on the changes in the property values.

In addition, the regular part of the recommendation of the last plus a space to determine, that URL+空格 is, to parse the link form, if the previous match will cause a dead 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 is hrefs in the original text, let's split//The text up and is work on the parts that don ' t has ur        LS 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, ' $ ');            }} Combinedreplacedtext = Splitinput.join (");        return combinedreplacedtext;            } else {replacedtext = Inputtext.replace (replacePattern1, ' $ ');            Replacedtext = Replacedtext.replace (replacePattern2, ' $1$2 ');            Replacedtext = Replacedtext.replace (ReplacePattern3, ' $ $ ');        return replacedtext; }    },

For a piece of text, you can run it 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.