JavaScript implementation URL address detection and Add URL link sample code _javascript tips

Source: Internet
Author: User
background:Write a simple chat system that emits a HTPP URL to implement the jump plus a tag.
Implementation code:
Copy Code code as follows:

String.prototype.httpHtml = function () {
var reg =/(http:\/\/|https:\/\/) (\w|=|\?| \.| \/|&|-) +)/g;
Return This.replace (Reg, ' <a target=_blank href= "$1$2" >$1$2</a> ");
};

Excerpts:
Implementation of automatically adding URL addresses
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 the 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 expression for the HTTP address is as follows (there may be omissions or inaccuracies, welcome correction):
var reg =/(http:\/\/|https:\/\/) (\w|=|\?| \.| \/|&|-) +)/g;
The previous part 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
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:
Copy Code code as follows:

var s = "blank";
s = S.replace (/^\s+ (. *?) \s+$/, "");
alert (s);
Related Article

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.