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