JS Filter HTML tags and  

Source: Internet
Author: User

Http://www.cnblogs.com/liszt/archive/2011/08/16/2140007.html

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 empty line            Str=str.replace (/&nbsp;/ig, ");//Remove &nbsp;            return str;    }

Well, my request has been met.

Now let's explain briefly the three regular expressions used (it should be explained that because I was just in touch, perhaps my explanation is not correct, for reference only):

The first one:/<\/? [^>]*>/g

In JS, the expression is preceded by "/", the/g, meaning is the global schema, meaning that the matching pattern is applied to the entire string, rather than the first match after the stop matching.

<\/? [^>]*> This separate explanation, where the second character "\" is a transfer character that is used to transfer the trailing "/" character. Match 0 or 1 exactly the character that precedes it. Note: This meta-character is not supported by all software. So <\/? Matches the "</" format in the HTML tag or the "<" format.

Again, [^>]*>. [] The meaning is:

The meaning of ^ is: match the beginning of a line. For example, the regular expression ^when in is able to match the start of the string "When in the course of human events", but does not match "what is in the". This means matching text that begins with "when".

* means: Match 0 or more of the characters just before it. For example, regular expressions. * means being able to match any number of characters

So [^>]* meaning is a character that matches >. so [^>] The pattern can be matched like this:

    1. Div
    2. I need the text </div
    3. P
    4. I need the text </p

* and the previous [^>] together to match the following characters:

    1. div> I need the text </div
    2. p> I need the text </p
    3. BR/

Plus the > in the back can match the following characters:

    1. Div> the text I need </div>
    2. P> the text I need </p>
    3. br/>

This completes the matching of a pair of HTML tags. (A lot of words, always feel that the match is a bit verbose, but do not know where in the end to wordy)

The second one:/[|] *\n/g: I didn't read it either.

The third:/&nbsp;/ig: is to directly find the &nbsp; character, the meaning of the following/ig is to make a case-insensitive lookup in global mode. G represents global, I means case insensitive.

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.