Background: Write a simple chat system, send Htpp Url to implement jump and add a tag.
Implementation code:
String.prototype.httpHtml = function(){
var reg = /(http://|https://)((w|=|?|.|/|&|-) )/g;
return this.replace( reg, '
$1$2');
};
Excerpt: The implementation of automatic addition of URL address
The implementation of automatic addition of URL address actually consists of just that: detection and replacement.
Detection "Detection" is to detect whether there is content in the text (string) that matches the http address. Obviously, this requires the use of regular expressions for verification. This working front end It can be done with both the front-end and the back-end. Here, we only talk about the front-end method, which is implemented using JavaScript.
The regular expression to verify the HTTP address is as follows (there may be omissions or inaccuracies, please correct me):
var reg = /(http://|https://)((w|=| ?|.|/|&|-) )/g;
The first part matches the URL string address starting with http or https, and the latter part matches some characters, such as English characters, underscore (_), and period (.) , question mark (?) and equal sign (=), connect dash (-), etc.
Replacement When it comes to the replacement function in JavaScript, the first thing that comes to mind is naturally the replace attribute. The power of the replace attribute is that it supports regular expressions and can match regular expressions. string to replace. For example, if we want to replace the spaces at both ends of the string, we can use a statement similar to the following:
var s = " blank ";
s = s.replace(/^s (.*?)s $/, "");
alert(s);
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn