JavaScript automatically adds links to text URL addresses by sharing methods

Source: Internet
Author: User
Tags regular expression

  This article mainly introduces the JavaScript automatically adds the link to the text URL address the method, the need friend may refer to the

The implementation of the URL address to add automatically is a little bit of content: detection and replacement.   Detection   "Detection" is the detection of text (string) within the content of HTTP address, obviously, this need to use the regular expression for verification, this work front-end and backstage can do, here, only the front-end method, using JavaScript implementation.   Verify that the regular expressions for HTTP addresses are as follows (there may be omissions or inaccuracies, please correct): The   code is as follows: var reg =/(http://|https://) (w|=|?|.| /|&|-) +)/g; The first part of     matches the URL string address at the beginning of http or HTTPS, followed by some characters, English characters, underscores (_), dots (.), question marks (?) and the equal sign (=), the connection short Line (-) and so on.   Replace www.jb51.net when it comes to the substitution function in JavaScript, the first thing to think about is the Replace property, which is powerful because it supports regular expressions and can replace strings that match the regular. For example, we would replace the spaces at each end of the string with a statement similar to the following: The code is as follows: var s = "blank"; s = S.replace (/^s+ (. *?) s+$/, ""); alert (s);     will get "blank", the two ends of the space is removed. In the same way, it is possible to replace the matching HTTP address with an HTTP address containing the HREF attribute in the <a> tags nested, which can match the Http,https,ftp,ftps and the URL of the IP address.   Code as follows: var URL =/(https?:/ /|ftps?:/ /)? ((d{1,3}.d{1,3}.d{1,3}.d{1,3}) (: [0-9]+)? | ([w]+.) (s+) (w{2,4}) (: [0-9]+)?] (/? ([w#!:.? +=&%@!-/]+))?/ig;     is also considered perfect for URL address matching. Using this expression I wrote two small functions to replace the URL of the user's message with clickable links, nothing too difficult, that is, using the JavaScript replace () function to implement the replacement URL is link:   code as follows:/**  * Java Scrit Version  * converts URL address to full a tag link code  */  var replaceurltolink = function (text) {        Text = Tex T.replace (URL, function (URL) {            var urltext = URL;         & nbsp   if (!url.match (' ^https?:/ /') {                URL = ' http://' + url;           & nbsp }             return ' + Urltext + ';        });           return text;    };  

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.