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; };