JavaScript filter domain name and HTML tag implementation code

Source: Internet
Author: User

Analysis

A complete domain name, by the root domain, top-level domain, level two, three ... Domain name composition, each level of domain name separated by point, each domain name by letter, number and minus sign (the first letter can not be a minus), case-insensitive, not more than 63.
Obviously, a single name can be matched by a regular expression [a-za-z0-9][-a-za-z0-9]{0,62}, and the complete domain name includes at least two names (such as Google.com, made up of Google and COM), and finally, a point that represents the root domain.

Regular expression that matches the full domain name:

The code is as follows Copy Code
[A-za-z0-9] [-a-za-z0-9] {0,62} (. [A-za-z0-9] [-a-za-z0-9] {0,62}) +.?

Our simplest version of JS regular filter to take the domain name

Domain name Regular expression JS version

The code is as follows Copy Code
function Doseo () {
if (!$ (' domain '). Value.match (/^ ([A-z0-9-]{1,}.)? [a-z0-9-] {2,}. ([A-z0-9-]{1,}.)? [A-z0-9] {2,}$/i)) {
Alert (' Hello ~ ~ ~ First, please enter the correct domain name:P ');
$ (' domain '). Value = ';
$ (' domain '). focus ();
Return
}

The above can be used to judge the domain name, then I want to filter the domain name how to see the example


After this friend test, he still prefers this effect (I personally or prefer to remove the space before detection), should he request the source to put out:

The code is as follows Copy Code

<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<title> Testing </title>
<body>
<script language= "JavaScript" >
function Checkurl (obj) {
var pattern =/cs*os*m|cs*n|ns*es*t|os*rs*g/gi;
if (Pattern.test (Obj.value)) {
Alert ("There are com|cn|net|org in the message, there is suspicion of advertising links, can not submit!" ");
Obj.value = "";
Obj.focus ();
return false;
}
else{
Do what you want to do.
}
}
</script>
<textarea name= "Content" cols= "rows=" wrap= "VIRTUAL" id= "content"
Onblur= "Javascript:checkurl (this);" ></textarea>

</body>


Online Find JS filter HTML title code is as follows:

The code is as follows Copy Code
function SetContent (str) {
str = str.replace (/</?[ ^>]*>/g, ""); Remove HTML tag
Str.value = str.replace (/[|) *n/g, ' n '); Remove line trailing blanks
str = str.replace (/n[s| |) *r/g, ' n '); Remove extra blank lines
return str;
}


The test found that this code can not filter out the Web page hollow characters (ie:&nbsp;). So I rebuilt it again:

The code is as follows Copy Code
function Removehtmltag (str) {
str = str.replace (/</?[ ^>]*>/g, ""); Remove HTML tag
str = str.replace (/[|) *n/g, ' n '); Remove line trailing blanks
str = str.replace (/n[s| |) *r/g, ' n '); Remove extra blank lines
Str=str.replace (/&nbsp;/ig, ')/Remove &nbsp;
return str;
}

Well, my request was reached.
So our filter domain name, HTML title and domain name is difficult to get done.

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.