JavaScript: automatically detecting url addresses and adding URL link example code _ javascript skills

Source: Internet
Author: User
Write a simple chat system and send an Htpp Url to add the tag. The following is a specific implementation. Do not miss it if you are interested. Background:Write a Simple Chat System and issue an Htpp Url to add the tag.
Implementation Code:

The Code is as follows:


String. prototype. httpHtml = function (){
Var reg =/(http: \/| https: \/) (\ w | = | \? | \. | \/| & |-) +)/G;
Return this. replace (reg, '$1 $2 ');
};


Excerpt:
Implementation of Automatic Adding of URL addresses
The implementation of automatic adding of URL addresses is actually something: Detection and replacement.

Detection
"Detection" is to check whether the text (string) contains content that conforms to the http address. Obviously, this requires regular expression verification. This can be done at the front end and back end. Here, only the front-end method is used for implementation in JavaScript.
The regular expression used to verify the HTTP address is as follows (which may be incorrect or inaccurate ):
Var reg =/(http: \/| https: \/) (\ w | = | \? | \. | \/| & |-) +)/G;
The first part matches the URL string address starting with http or https. The latter part matches some characters, including English characters, underscores (_), periods (.), and question marks (?) And equal signs (=), short connections (-), etc.

Replace
When it comes to the replacement function in JavaScript, the first thing that comes to mind is the replace attribute. The powerful feature of the replace attribute is that it supports regular expressions and can replace regular strings. For example, to replace the spaces at both ends of a string, you can use a statement similar to the following:

The Code is 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.