Regular Go Space method

Source: Internet
Author: User

<script>
function Delhtmltag (str)
{
var str=str.replace (/<\/?[ ^>]*>/gim, "");//Remove all HTML tags
var result=str.replace (/(^\s+) | ( \s+$)/g, "");//Remove space before and after
Return result.replace (/\s/g, "");//Remove the middle space of the article
}
</script>

The above method is the way to remove the space ~ ~ so we often encounter similar requirements, you never know what users will lose, then you can only try to avoid the input you do not want. The above changes can also become a verification of the existence

Spaces give a hint

Verify that the content contains spaces
function Checktextspace (obj,temp) {
var reg=/(^\s+) | (\s+$)/g;
var alertvalue= "input contains spaces, please enter new!";
Temp is used to identify whether the content is allowed to exist in spaces 1 for the existence of 0 for non-existent
if (temp==1) {
reg=/(^\s{5,}) | (\s{5,}$) | (\s{5,})/g;
Alertvalue= "content of more than 5 consecutive input spaces, please re-enter! ";
}
if (Reg.test (Obj.value)) {
alert (Alertvalue);
Obj.focus ();
return false;
}
}

The above code is I met a need to change, you can also make your own changes, I would simply explain (master do not spray):

^ Start of matching string

$ match End of string

/s matches any white space character


/(^\s+) | (\s+$)/g This is the match content contains a space, regardless of the front or center, can match to

/(^\s{5,}) | (\s{5,}$) | (\s{5,})/g This regular is I do another validation change, mainly to match the number of consecutive input spaces

\s{5,} This representative matches 5 or more times

\s* the representative repeats 0 or more times.

\s+ the representative repeats 1 or more times.

\s, the representative repeats 0 or 1 times.

Regular Go Space method

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.