Background:Write a Simple Chat System and issue an Htpp Url to add the tag.
Implementation Code:
Copy codeThe Code is 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> ');
};
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:
Copy codeThe Code is as follows:
Var s = "blank ";
S = s. replace (/^ \ s + (.*?) \ S + $ /,"");
Alert (s );